-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclipper.cpp
More file actions
106 lines (85 loc) · 3.12 KB
/
Copy pathclipper.cpp
File metadata and controls
106 lines (85 loc) · 3.12 KB
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
#include "clipper.h"
#include <list>
vec3d intersectionOfLineAndPlane(vec3d &pointOnPlane, vec3d &normalVectorToPlane, vec3d &firstEndpoint, vec3d &lastEndpoint)
{
vec3d lineDirection = lastEndpoint - firstEndpoint;
float planeEqnConstant = dotProduct(normalVectorToPlane, pointOnPlane);
float intersectionTime = (planeEqnConstant - dotProduct(normalVectorToPlane, firstEndpoint)) / (dotProduct(normalVectorToPlane, lineDirection));
vec3d intersectionPoint = intersectionTime * lineDirection + firstEndpoint;
return intersectionPoint;
}
bool onCorrectSideOfPlane(vec3d &pointOnPlane, vec3d &normalVectorToPlane, vec3d &somePoint)
{
return (dotProduct(normalVectorToPlane, somePoint - pointOnPlane) >= 0);
}
int numVerticesOnCorrectSide(vec3d &pointOnPlane, vec3d &normalVectorToPlane, triangle &tri)
{
return (onCorrectSideOfPlane(pointOnPlane, normalVectorToPlane, tri.p[0]) +
onCorrectSideOfPlane(pointOnPlane, normalVectorToPlane, tri.p[1]) +
onCorrectSideOfPlane(pointOnPlane, normalVectorToPlane, tri.p[2])
);
}
void rotateTriangle(triangle &tri)
{
vec3d temp = tri.p[0];
tri.p[0] = tri.p[1];
tri.p[1] = tri.p[2];
tri.p[2] = temp;
}
void clipByPlane(vec3d &pointOnPlane, vec3d &normalVectorToPlane, std::list<triangle> &tris, bool clipping_debug)
{
for (std::list<triangle>::iterator i = tris.begin(); i != tris.end(); )
{
triangle &tri = *i;
int numVerts = numVerticesOnCorrectSide(pointOnPlane, normalVectorToPlane, tri);
if (numVerts == 3)
{
i++;
}
else if (numVerts == 2)
{
while (onCorrectSideOfPlane(pointOnPlane, normalVectorToPlane, tri.p[0])) // Make sure that vertex 0 of our triangle is the one on the wrong side
rotateTriangle(tri);
vec3d intersection1 = intersectionOfLineAndPlane(pointOnPlane, normalVectorToPlane, tri.p[0], tri.p[1]);
vec3d intersection2 = intersectionOfLineAndPlane(pointOnPlane, normalVectorToPlane, tri.p[0], tri.p[2]);
triangle tri1{ tri.p[1], tri.p[2], intersection1 };
triangle tri2{ tri.p[2], intersection2, intersection1 };
tri1.color = tri.color;
tri1.symbol = tri.symbol;
tri2.color = tri.color;
tri2.symbol = tri.symbol;
if (clipping_debug){
tri1.color = FG_RED;
tri1.symbol = PIXEL_SOLID;
tri2.color = FG_YELLOW;
tri2.symbol = PIXEL_SOLID; }
i = tris.erase(i);
tris.insert(i, tri1);
tris.insert(i, tri2);
}
else if (numVerts == 1)
{
triangle clippedTriangle;
while (!onCorrectSideOfPlane(pointOnPlane, normalVectorToPlane, tri.p[0]))
rotateTriangle(tri);
vec3d intersection1 = intersectionOfLineAndPlane(pointOnPlane, normalVectorToPlane, tri.p[0], tri.p[1]);
vec3d intersection2 = intersectionOfLineAndPlane(pointOnPlane, normalVectorToPlane, tri.p[0], tri.p[2]);
triangle tri1{ tri.p[0], intersection1, intersection2 };
tri1.color = tri.color;
tri1.symbol = tri.symbol;
if (clipping_debug) {
tri1.color = FG_GREEN;
tri1.symbol = PIXEL_SOLID;}
i = tris.erase(i);
tris.insert(i, tri1);
}
else if (numVerts == 0)
{
i = tris.erase(i);
}
else {
std::cout << "That's not supposed to happen..." << std::endl;
assert(1 == 0);
}
}
}