-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelements.cpp
599 lines (514 loc) · 13.1 KB
/
elements.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*
Name: elements.cpp
Author: Blakey Wu
Date: 10/6/2019
*/
#include "elements.h"
#define _ASSERTION
mat::mat(int m, int n): m(m), n(n){
this->size = m * n;
this->buffer = NULL;
#ifdef _ASSERTION
assert(m > 0 && n > 0);
#endif
this->buffer = new float[m * n];
for (int i = 0; i < this->size; i++){
this->buffer[i] = .0f;
}
}
mat::~mat(){
delete[] this->buffer;
}
mat::mat(const mat& other): m(other.m), n(other.n), size(other.size){
this->buffer = new float[m * n];
for (int i = 0; i < other.size; i++){
this->buffer[i] = other.buffer[i];
}
}
// TODO: if this is used for pixel-wise operation, it should be optimized.
mat mat::operator * (const mat& other)const{
#ifdef _ASSERTION
assert(this->n == other.m);
#endif
mat res = mat(this->m, other.n);
for (int i = 0; i < this->m; i++){
for (int j = 0; j < other.n; j++){
float counter = 0.0f;
for (int k = 0; k < this->n; k++){
counter += this->buffer[i * this->n + k] * other.buffer[k * other.n + j];
}
res.buffer[i * other.n + j] = counter;
}
}
return res;
}
mat mat::operator * (float scale) const{
mat res = mat(this->m, this->n);
for (int i = 0; i < this->size; i++){
res.buffer[i] = this->buffer[i] * scale;
}
return res;
}
mat mat::operator / (float scale) const{
#ifdef _ASSERTION
assert(scale != .0f);
#endif
mat res = mat(this->m, this->n);
for (int i = 0; i < this->size; i++){
res.buffer[i] = this->buffer[i] / scale;
}
return res;
}
mat mat::operator + (const mat& other)const{
#ifdef _ASSERTION
assert(this->m == other.m && this->n == other.n);
#endif
mat res = mat(this->m, this->n);
for (int i = 0; i < this->size; i++){
res.buffer[i] = this->buffer[i] + other.buffer[i];
}
return res;
}
void mat::operator += (const mat& other){
#ifdef _ASSERTION
assert(this->m == other.m && this->n == other.n);
#endif
for (int i = 0; i < this->size; i++){
this->buffer[i] += other.buffer[i];
}
}
mat mat::operator - (const mat& other)const{
#ifdef _ASSERTION
assert(this->m == other.m && this->n == other.n);
#endif
mat res = mat(this->m, this->n);
for (int i = 0; i < this->size; i++){
res.buffer[i] = this->buffer[i] - other.buffer[i];
}
return res;
}
bool mat::operator < (const mat& other)const{
for (int i = 0; i < size; i++){
if (buffer[i] >= other.buffer[i]){
return false;
}
}
return true;
}
bool mat::operator > (const mat& other)const{
for (int i = 0; i < size; i++){
if (buffer[i] <= other.buffer[i]){
return false;
}
}
return true;
}
bool mat::operator <= (const mat& other)const{
for (int i = 0; i < size; i++){
if (buffer[i] > other.buffer[i]){
return false;
}
}
return true;
}
bool mat::operator >= (const mat& other)const{
for (int i = 0; i < size; i++){
if (buffer[i] < other.buffer[i]){
return false;
}
}
return true;
}
void mat::operator -= (const mat& other){
#ifdef _ASSERTION
assert(this->m == other.m && this->n == other.n);
#endif
for (int i = 0; i < this->size; i++){
this->buffer[i] -= other.buffer[i];
}
}
void mat::operator *= (float scale){
for (int i = 0; i < this->size; i++){
this->buffer[i] *= scale;
}
}
void mat::operator /= (float scale){
#ifdef _ASSERTION
assert(scale != .0f);
#endif
for (int i = 0; i < this->size; i++){
this->buffer[i] /= scale;
}
}
void mat::operator = (const mat& other){
if (this->size != other.size){
delete[] this->buffer;
buffer = new float[other.size];
this->size = other.size;
this->m = other.m;
this->n = other.n;
}
for (int i = 0; i < this->size; i++){
this->buffer[i] = other.buffer[i];
}
}
float mat::get(int index)const{
#ifdef _ASSERTION
assert(index < size); // float mat::get(int index)const
#endif
return buffer[index];
}
float &mat::operator[] (int index){
#ifdef _ASSERTION
assert(index < size);
#endif
return buffer[index];
}
float mat::norm2()const{
float counter = .0f;
float number_i;
for (int i = 0; i < this->size; i++){
number_i = this->buffer[i];
counter += number_i * number_i;
}
return std::sqrt(counter);
}
float mat::dot(const mat& other)const{
#ifdef _ASSERTION
assert(this->m == other.m && this->n == other.n);
#endif
float counter = 0;
for (int i = 0; i < this->size; i++){
counter += this->buffer[i] * other.buffer[i];
}
return counter;
}
void mat::print()const{
std::cout << "mat" << this->m << "*" << this->n << " ";
for (int i = 0; i < this->m; i++){
std::cout << " ";
for (int j = 0; j < this->n; j++){
std::cout << this->buffer[i * this->n + j] << " ";
}
}
}
mat11::mat11(const mat& other): mat(other){
#ifdef _ASSERTION
assert(other.getM() == 1 && other.getN() == 1);
#endif
}
mat21::mat21(float x, float y): mat(2, 1){
this->buffer[0] = x;
this->buffer[1] = y;
}
mat21::mat21(const mat& other): mat(other){
#ifdef _ASSERTION
assert(other.getM() == 2 && other.getN() == 1);
#endif
}
mat31::mat31(float x, float y, float z): mat(3, 1){
this->buffer[0] = x;
this->buffer[1] = y;
this->buffer[2] = z;
}
mat31::mat31(const mat& other): mat(other){
#ifdef _ASSERTION
assert(other.getM() == 3 && other.getN() == 1);
#endif
}
mat12::mat12(float x, float y): mat(1, 2){
this->buffer[0] = x;
this->buffer[1] = y;
}
mat12::mat12(const mat& other): mat(other){
#ifdef _ASSERTION
assert(other.getM() == 1 && other.getN() == 2);
#endif
}
mat22::mat22(float x11, float x12, float x21, float x22): mat(2, 2){
this->buffer[0] = x11;
this->buffer[1] = x12;
this->buffer[2] = x21;
this->buffer[3] = x22;
}
mat22::mat22(const mat& other): mat(other){
#ifdef _ASSERTION
assert(other.getM() == 2 && other.getN() == 2);
#endif
}
mat33::mat33(float x11, float x12, float x13,
float x21, float x22, float x23,
float x31, float x32, float x33): mat(3, 3){
this->buffer[0] = x11;
this->buffer[1] = x12;
this->buffer[2] = x13;
this->buffer[3] = x21;
this->buffer[4] = x22;
this->buffer[5] = x23;
this->buffer[6] = x31;
this->buffer[7] = x32;
this->buffer[8] = x33;
}
mat33::mat33(const mat& other): mat(other){
#ifdef _ASSERTION
assert(other.getM() == 3 && other.getN() == 3);
#endif
}
mat33::mat33(const mat31 & m1, const mat31 & m2, const mat31 & m3): mat(3, 3){
for (int i = 0; i < 3; i++){
this->buffer[3 * i + 0] = m1.get(i);
this->buffer[3 * i + 1] = m2.get(i);
this->buffer[3 * i + 2] = m3.get(i);
}
}
mat33 mat33::inverse()const{
float* m = buffer;
float a0 = m[4] * m[8] - m[5] * m[7];
float a1 = m[3] * m[8] - m[5] * m[6];
float a2 = m[3] * m[7] - m[4] * m[6];
float det = m[0] * a0 - m[1] * a1 + m[2] * a2;
if (det == 0){
/* if det == 0, then can not inverse */
return mat33();
}
a0 /= det;
a1 /= det;
a2 /= det;
float a3 = (m[1] * m[8] - m[2] * m[7]) / det;
float a4 = (m[0] * m[8] - m[2] * m[6]) / det;
float a5 = (m[0] * m[7] - m[1] * m[6]) / det;
float a6 = (m[1] * m[5] - m[2] * m[4]) / det;
float a7 = (m[0] * m[5] - m[2] * m[3]) / det;
float a8 = (m[0] * m[4] - m[1] * m[3]) / det;
return mat33(
a0, -a3, a6,
-a1, a4, -a7,
a2, -a5, a8
);
}
eye22::eye22(): mat22(1.0f, .0f, .0f, 1.0f){}
eye33::eye33(): mat33(1.0f, .0f, .0f, .0f, 1.0f, .0f, .0f, .0f, 1.0f){}
point::point(float x, float y, float r, float g, float b): mat21(x, y), r(r), g(g), b(b){
}
point::point(const mat21 & other): mat21(other){}
point point::getCentroid() const{
return *this;
}
void point::transport(const mat21 & v){
*this += v;
}
void point::linear_transform(const mat22 & r, const point & origin){
*this -= origin;
float x = this->r, y = g, z = b;
(*this) = mat21(r * (*this));
this->r = x;
g = y;
b = z;
*this += origin;
}
void point::print()const{
std::cout << "(" << this->buffer[0] << ", " << this->buffer[1] << ")" << std::endl;
}
line::line(const point& p1, const point& p2): p1(p1), p2(p2){
}
line::line(const line& other): p1(other.p1), p2(other.p2){
}
/* need to be optimized if it is called frequently between points' update */
float line::getLength()const{
return (this->p1 - this->p2).norm2();
}
/* need to be optimized if it is called frequently between points' update */
mat21 line::getDirection()const{
mat21 delta = this->p2 - this->p1;
return delta / delta.norm2();
}
/* need to be optimized if it is called frequently between points' update */
point line::getCentroid() const{
return mat21((p1 + p2) / 2.0f);
}
void line::transport(const mat21 & v){
p1 += v;
p2 += v;
}
void line::linear_transform(const mat22 & r, const point & origin){
p1.linear_transform(r, origin);
p2.linear_transform(r, origin);
}
bool line::intersect_with(const line & other)const{
mat21 t1 = other.p1 - this->p1;
mat21 t2 = other.p2 - this->p1;
mat21 t3 = this->p2 - this->p1;
float c1 = t3.cross(t1) * t3.cross(t2);
t1 = this->p1 - other.p1;
t2 = this->p2 - other.p1;
t3 = other.p2 - other.p1;
float c2 = t3.cross(t1) * t3.cross(t2);
if (c1 < 0 && c2 < 0){
return true;
}
if (c1 > 0 || c2 > 0){
return false;
}
if ((c1 * c2 == 0) && (c1 + c2 < 0)){
return true;
}
float l1 = (this->getCentroid() - other.getCentroid()).norm2();
float l2 = this->getLength() + other.getLength();
if (2 * l1 > l2){
return false;
}
else{
return true;
}
}
void line::print()const{
std::cout << "Line: " << std::endl;
std::cout << "P1: ";
p1.print();
std::cout << "P2: ";
p2.print();
}
polygon::polygon(const std::vector<point>& p_set){
#ifdef _ASSERTION
assert(p_set.size() > 0);
#endif
std::vector<line> lines;
for (auto p = p_set.begin(); p < p_set.end() - 1; p++){
lines.push_back(line(*p, *(p+1)));
}
lines.push_back(line(*p_set.rbegin(), *p_set.begin()));
bool ok = true;
for (auto l = lines.begin() + 1; l < lines.end() - 1; l++){
for (auto s = lines.begin(); s < l - 1; s++){
if (l->intersect_with(*s)){
std::cout << "Error: " << std::endl;
l->print();
std::cout << " intersects with: ";
s->print();
ok = false;
break;
}
}
if (l != (lines.end() - 2) && lines.rbegin()->intersect_with(*l)){
std::cout << "Error: " << std::endl;
l->print();
std::cout << " intersects with: ";
lines.rbegin()->print();
ok = false;
break;
}
if (!ok){
throw "Error: intersection";
}
}
if (ok){
this->points = p_set;
}
}
polygon::polygon(const polygon & other): points(other.points){
}
/* need optimization if called frequently */
point polygon::getCentroid() const{
mat21 counter(.0f, .0f);
for (auto p : this->points){
counter += p;
}
return point(mat21(counter / this->points.size()));
}
void polygon::transport(const mat21 & v){
for (auto p = this->points.begin(); p < this->points.end(); p++){
p->transport(v);
}
}
void polygon::linear_transform(const mat22 & r, const point & origin){
for (auto p = this->points.begin(); p < this->points.end(); p++){
p->linear_transform(r, origin);
}
}
void polygon::print()const{
for (auto p = this->points.begin(); p < this->points.end(); p++){
p->print();
}
}
point_3d::point_3d(float x, float y, float z, float r, float g, float b): mat31(x, y, z), r(r), g(g), b(b){
}
point_3d::point_3d(const mat31 & other): mat31(other){}
point_3d point_3d::getCentroid() const{
return *this;
}
void point_3d::transport(const mat31 & v){
*this += v;
}
void point_3d::linear_transform(const mat33 & r, const point_3d & origin){
*this -= origin;
float R = this->r, G = this->g, B = this->b;
(*this) = mat31(r * (*this));
this->r = R;
this->g = G;
this->b = B;
*this += origin;
}
void point_3d::print()const{
std::cout << "(" << this->buffer[0] << ", " << this->buffer[1] << ", " << this->buffer[2] << ")" << std::endl;
}
line_3d::line_3d(const point_3d& p1, const point_3d& p2): p1(p1), p2(p2){
}
line_3d::line_3d(const line_3d& other): p1(other.p1), p2(other.p2){
}
/* need to be optimized if it is called frequently between points' update */
float line_3d::getLength()const{
return (this->p1 - this->p2).norm2();
}
/* need to be optimized if it is called frequently between points' update */
mat31 line_3d::getDirection()const{
mat31 delta = this->p2 - this->p1;
return delta / delta.norm2();
}
/* need to be optimized if it is called frequently between points' update */
point_3d line_3d::getCentroid() const{
return mat31((p1 + p2) / 2.0f);
}
void line_3d::transport(const mat31 & v){
p1 += v;
p2 += v;
}
void line_3d::linear_transform(const mat33 & r, const point_3d & origin){
p1.linear_transform(r, origin);
p2.linear_transform(r, origin);
}
void line_3d::print()const{
std::cout << "Line_3d: " << std::endl;
std::cout << "P1: ";
p1.print();
std::cout << "P2: ";
p2.print();
}
polyhedron::polyhedron(const std::vector<point_3d> points, std::vector<int> starts, std::vector<int> ends):
points(points), starts(starts), ends(ends){}
polyhedron::polyhedron(const polyhedron & other):
points(other.points), starts(other.starts), ends(other.ends){}
point_3d polyhedron::getCentroid() const{
mat31 counter(0, 0, 0);
for (auto p : this->points){
counter += mat31(p);
}
counter /= this->points.size();
return counter;
}
void polyhedron::transport(const mat31 & v){
for (auto p = this->points.begin(); p < this->points.end(); p++){
p->transport(v);
}
}
void polyhedron::linear_transform(const mat33 & r, const point_3d & origin){
for (auto p = this->points.begin(); p < this->points.end(); p++){
p->linear_transform(r, origin);
}
}
void polyhedron::print()const{
std::cout << "polyhedron: " << std::endl;
for (auto p = this->points.begin(); p < this->points.end(); p++){
p->print();
}
std::cout << "polyhedron end." << std::endl;
}