-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLineEquations.cpp
More file actions
127 lines (104 loc) · 3.69 KB
/
LineEquations.cpp
File metadata and controls
127 lines (104 loc) · 3.69 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "StdAfx.h"
#include "LineEquations.h"
#include "MatrixEquations.h"
#include <iostream>
//Orders Vertices for LEE calculations in Object Order
void LineEquations::OrderVerts(GzCoord* ptr, GzCoord* normalPtr, GzTextureIndex* uvList) {
//GzCoord temp;
float* currentPtr, *nextVal;
bool topBottomRelationship = false;
float topBottomVal;
float otherValue = 0;
//Order verts by Y
for (int i = 0; i < 2; i++) {
for (int j = i + 1; j < 3; j++) {
currentPtr = (float*)ptr[i];
currentPtr++; //set to Y val
nextVal = (float*)ptr[j];
nextVal++; //Set to yval
//If current is larger than nextval, swap
if (*currentPtr > *nextVal) {
std::swap(ptr[i], ptr[j]);
std::swap(normalPtr[i], normalPtr[j]);
std::swap(uvList[i], uvList[j]);
}
else if (*currentPtr == *nextVal) {
topBottomRelationship = true;
topBottomVal = *currentPtr;
float* findVal = (float*)ptr[0];
findVal++;
for (int h = 0; h < 3; h++) {
if (*findVal != *currentPtr) {
otherValue = *findVal;
findVal += 3;
}
}
}
}
}
if (topBottomRelationship) {
if (otherValue > topBottomVal) {
currentPtr = (float*)ptr[0];
nextVal = (float*)ptr[1];
if (*nextVal > *currentPtr) {
std::swap(ptr[0], ptr[1]);
std::swap(normalPtr[0], normalPtr[1]);
std::swap(uvList[0], uvList[1]);
}
}
else {
currentPtr = (float*)ptr[1];
nextVal = (float*)ptr[2];
if (*nextVal < *currentPtr) {
std::swap(ptr[1], ptr[2]);
std::swap(normalPtr[1], normalPtr[2]);
std::swap(uvList[1], uvList[2]);
}
}
}
else {
//Check LR relationship
currentPtr = (float*)ptr[1];
currentPtr++; //Set to yVal
float y = *currentPtr; //Retain middle vert Y value
currentPtr--; //Set to xVal
float coefficients[3];
GetLineCoefficients(ptr[0], ptr[2], coefficients);
float xVal = -1 * (coefficients[1] * y + coefficients[2]) / coefficients[0];
if (xVal > *currentPtr) {
std::swap(ptr[0], ptr[1]);
std::swap(normalPtr[0], normalPtr[1]);
std::swap(uvList[0], uvList[1]);
}
}
}
//returns line coefficients
void LineEquations::GetLineCoefficients(float* tailVert, float* headVert, float* returnArray) {
returnArray[0] = headVert[1] - tailVert[1]; //A = dY
returnArray[1] = tailVert[0] - headVert[0]; //B = -(dX)
returnArray[2] = (headVert[0] - tailVert[0]) * tailVert[1] - returnArray[0] * tailVert[0]; //C = (dX * Y) - (dY * X)
}
//LEE, if negative its 'inside' the line, need three evals for a triangle
bool LineEquations::EvaluatePointByLine(float* lineCoefficients, int x, int y) {
float val = lineCoefficients[0] * x + lineCoefficients[1] * y + lineCoefficients[2];
if (val <= 0) return true; //need to do 0 check here.
else { return false; }
}
inline float CalculateD(float* otherCoefficients, float* vert) {
return -1 * (otherCoefficients[0] * vert[0] + otherCoefficients[1] * vert[1] + otherCoefficients[2] * vert[2]);
}
void LineEquations::GetPlaneCoefficients(float* coordOne, float* coordTwo, float* coordThree, float* returnVal) {
GzCoord vecOne = { coordTwo[0] - coordOne[0], coordTwo[1] - coordOne[1], coordTwo[2] - coordOne[2] };
GzCoord vecTwo = { coordThree[0] - coordTwo[0], coordThree[1] - coordTwo[1], coordThree[2] - coordTwo[2] };
MatrixEquations::CrossProduct((float*)vecOne, (float*)vecTwo, returnVal);
returnVal[3] = CalculateD(returnVal, coordOne);
}
float LineEquations::InterpolateZ(float* coefficients, int x, int y) {
return (-1 * (coefficients[0] * x + coefficients[1] * y + coefficients[3]) / coefficients[2]);
}
float LineEquations::InterpolateZFloat(float* coefficients, float x, float y) {
if (coefficients[2] == 0) {
return 1;
}
return (-1 * (coefficients[0] * x + coefficients[1] * y + coefficients[3]) / coefficients[2]);
}