-
Notifications
You must be signed in to change notification settings - Fork 1
/
stlcompare.cpp
428 lines (332 loc) · 8.8 KB
/
stlcompare.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Copyright (c) 2014 Andranik Abrahamyan
#include "stlfile.h"
#include "stlsphere.h"
#include "stlcompare.h"
StlCompare::StlCompare()
{
m_stlFile1 = 0;
m_stlFile2 = 0;
m_deleteStl = false;
}
StlCompare::~StlCompare()
{
if(m_deleteStl) {
if (m_stlFile1)
delete m_stlFile1;
if (m_stlFile2)
delete m_stlFile2;
}
}
char StlCompare::init(std::string &fileName1, std::string &fileName2)
{
m_deleteStl = true;
m_stlFile1 = new StlFile;
if (!m_stlFile1->open (fileName1)) {
::std::cerr << "The file " << fileName1 << " not a correct STL file." << ::std::endl;
return -1;
}
if (fileName2.size() > 0) {
m_stlFile2 = new StlFile;
if(!m_stlFile2->open (fileName2)) {
::std::cerr << "The file " << fileName2 << " not a correct STL file." << ::std::endl;
return -1;
}
}
return getHuffman_Code();
}
char StlCompare::init(StlFile* stlFile1, StlFile* stlFile2)
{
m_stlFile1 = stlFile1;
m_stlFile2 = stlFile2;
return getHuffman_Code();
}
// compare 2 stl files Huffman_Code, returnd by procent
char StlCompare::getHuffman_Code()
{
char res = 0;
if(m_stlFile2 == 0)
return res;
std::vector<StlSphere::NormalFrequency>* freq1;
std::vector<StlSphere::NormalFrequency>* freq2;
freq1 = m_stlFile1->m_stlSphere->getNormalFrequency ();
freq2 = m_stlFile2->m_stlSphere->getNormalFrequency ();
int total_freq1 = 0;
int total_freq2 = 0;
for (int j = 0; j < freq2->size() ; j++)
total_freq2 += freq2->at(j).frequency ;
int common_freq = 0;
for (int i = 0; i < freq1->size() ; i++) {
short x_pos = freq1->at(i).x_pos ;
short y_pos = freq1->at(i).y_pos ;
short z_pos = freq1->at(i).z_pos ;
int frequency = freq1->at(i).frequency ;
total_freq1 += frequency;
for (int j = 0; j < freq2->size() ; j++) {
if( freq2->at(j).x_pos == x_pos &&
freq2->at(j).y_pos == y_pos &&
freq2->at(j).z_pos == z_pos )
{
common_freq += std::min(freq2->at(j).frequency, frequency);
break;
}
}
}
float res1 = (common_freq*100)/total_freq1 ;
float res2 = (common_freq*100)/total_freq2 ;
res1 += res2;
res1 /= 2;
res = (char)res1;
return res;
}
/*
#include <iostream>
#include <vector>
#include <map>
#include <list>
#include <fstream>
using namespace std;
class Node
{
public:
int a;
char c;
Node *left, *right;
Node(){left=right=NULL;}
Node(Node *L, Node *R)
{ left = L;
right = R;
a = L->a + R->a; }
};
struct MyCompare
{
bool operator()(const Node* l, const Node* r) const { return l->a < r->a; }
};
vector<bool> code;
map<char,vector<bool> > table;
void BuildTable(Node *root)
{
if (root->left!=NULL)
{ code.push_back(0);
BuildTable(root->left);}
if (root->right!=NULL)
{ code.push_back(1);
BuildTable(root->right);}
if (root->c) table[root->c]=code;
code.pop_back();
}
int main (int argc, char *argv[])
{
////// считаем частоты символов
ifstream f("1.txt");
map<char,int> m;
while (!f.eof())
{ char c = f.get();
m[c]++;}
////// записываем начальные узлы в список list
list<Node*> t;
for( map<char,int>::iterator itr=m.begin(); itr!=m.end(); ++itr)
{
Node *p = new Node;
p->c = itr->first;
p->a = itr->second;
t.push_back(p); }
////// создаем дерево
while (t.size()!=1)
{
t.sort(MyCompare());
Node *SonL = t.front();
t.pop_front();
Node *SonR = t.front();
t.pop_front();
Node *parent = new Node(SonL,SonR);
t.push_back(parent);
}
Node *root = t.front(); //root - указатель на вершину дерева
////// создаем пары 'символ-код':
BuildTable(root);
////// Выводим коды в файл output.txt
f.clear(); f.seekg(0); // перемещаем указатель снова в начало файла
ofstream g("output.txt", ios::out | ios::binary);
int count=0; char buf=0;
while (!f.eof())
{ char c = f.get();
vector<bool> x = table[c];
for(int n=0; n<x.size(); n++)
{buf = buf | x[n]<<(7-count);
count++;
if (count==8) { count=0; g<<buf; buf=0; }
}
}
f.close();
g.close();
///// считывание из файла output.txt и преобразование обратно
ifstream F("output.txt", ios::in | ios::binary);
setlocale(LC_ALL,"Russian"); // чтоб русские символы отображались в командной строке
Node *p = root;
count=0; char byte;
byte = F.get();
while(!F.eof())
{ bool b = byte & (1 << (7-count) ) ;
if (b) p=p->right; else p=p->left;
if (p->left==NULL && p->right==NULL) {cout<<p->c; p=root;}
count++;
if (count==8) {count=0; byte = F.get();}
}
F.close();
return 0;
}
//Programe For Huffman Code
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAX_TREE_HT 100
struct MH_Node
{
char character;
unsigned frequency;
struct MH_Node *l, *r;
};
struct M_Heap
{
unsigned size;
unsigned space;
struct MH_Node **array;
};
struct MH_Node* newNode(char character, unsigned frequency)
{
struct MH_Node* temp = (struct MH_Node*) malloc(sizeof(struct MH_Node));
temp->l = temp->r = NULL;
temp->character = character;
temp->frequency = frequency;
return temp;
}
struct M_Heap* createM_Heap(unsigned space)
{
struct M_Heap* M_Heap = (struct M_Heap*) malloc(sizeof(struct M_Heap));
M_Heap->size = 0;
M_Heap->space = space;
M_Heap->array = (struct MH_Node**)malloc(M_Heap->space * sizeof(struct MH_Node*));
return M_Heap;
}
void swapMH_Node(struct MH_Node** a, struct MH_Node** b)
{
struct MH_Node* t = *a;
*a = *b;
*b = t;
}
void M_Heapify(struct M_Heap* M_Heap, int idx)
{
int smallest = idx;
int l = 2 * idx + 1;
int r = 2 * idx + 2;
if (l < M_Heap->size && M_Heap->array[l]->frequency < M_Heap->array[smallest]->frequency)
smallest = l;
if (r < M_Heap->size && M_Heap->array[r]->frequency < M_Heap->array[smallest]->frequency)
smallest = r;
if (smallest != idx)
{
swapMH_Node(&M_Heap->array[smallest], &M_Heap->array[idx]);
M_Heapify(M_Heap, smallest);
}
}
int isSizeOne(struct M_Heap* M_Heap)
{
return (M_Heap->size == 1);
}
struct MH_Node* extractMin(struct M_Heap* M_Heap)
{
struct MH_Node* temp = M_Heap->array[0];
M_Heap->array[0] = M_Heap->array[M_Heap->size - 1];
--M_Heap->size;
M_Heapify(M_Heap, 0);
return temp;
}
void insertM_Heap(struct M_Heap* M_Heap, struct MH_Node* MH_Node)
{
int i = M_Heap->size - 1;
++M_Heap->size;
while (i && MH_Node->frequency < M_Heap->array[(i - 1)/2]->frequency)
{
M_Heap->array[i] = M_Heap->array[(i - 1)/2];
i = (i - 1)/2;
}
M_Heap->array[i] = MH_Node;
}
void buildM_Heap(struct M_Heap* M_Heap)
{
int n = M_Heap->size - 1;
int i;
for (i = (n - 1) / 2; i >= 0; --i)
M_Heapify(M_Heap, i);
}
void printArr(int arr[], int n)
{
int i;
for (i = 0; i < n; ++i)
printf("%d", arr[i]);
printf("\n");
}
int isLeaf(struct MH_Node* root)
{
return !(root->l) && !(root->r) ;
}
struct M_Heap* createAndBuildM_Heap(char character[], int frequency[], int size)
{
int i;
struct M_Heap* M_Heap = createM_Heap(size);
for (i = 0; i < size; ++i)
M_Heap->array[i] = newNode(character[i], frequency[i]);
M_Heap->size = size;
buildM_Heap(M_Heap);
return M_Heap;
}
struct MH_Node* buildHuffmanTree(char character[], int frequency[], int size)
{
struct MH_Node *l, *r, *top;
struct M_Heap* M_Heap = createAndBuildM_Heap(character, frequency, size);
while (!isSizeOne(M_Heap))
{
l = extractMin(M_Heap);
r = extractMin(M_Heap);
top = newNode('$', l->frequency + r->frequency);
top->l = l;
top->r = r;
insertM_Heap(M_Heap, top);
}
return extractMin(M_Heap);
}
void printCodes(struct MH_Node* root, int arr[], int top)
{
if (root->l)
{
arr[top] = 0;
printCodes(root->l, arr, top + 1);
}
if (root->r)
{
arr[top] = 1;
printCodes(root->r, arr, top + 1);
}
if (isLeaf(root))
{
printf("%c: ", root->character);
printArr(arr, top);
}
}
void HuffmanCodes(char character[], int frequency[], int size)
{
struct MH_Node* root = buildHuffmanTree(character, frequency, size);
int arr[MAX_TREE_HT], top = 0;
printCodes(root, arr, top);
}
void main()
{
char arr[] = {'a', 'b', 'c', 'd', 'e', 'f'};
int frequency[] = {5, 9, 12, 13, 16, 45};
int size;
clrscr();
size = sizeof(arr)/sizeof(arr[0]);
HuffmanCodes(arr, frequency, size);
getch();
}
*/