From f66f9f66194f6d1f1a0985d2f2e4654ec3bbda26 Mon Sep 17 00:00:00 2001 From: <> Date: Sun, 2 Apr 2017 07:04:09 -0700 Subject: [PATCH] Now compiles on GCC & removed ckmath --- ReflexToQ3/main.cpp | 111 ++++++++++++++++++++++++++++++-------------- 1 file changed, 77 insertions(+), 34 deletions(-) diff --git a/ReflexToQ3/main.cpp b/ReflexToQ3/main.cpp index 257ce81..e155347 100644 --- a/ReflexToQ3/main.cpp +++ b/ReflexToQ3/main.cpp @@ -11,42 +11,85 @@ // Structs struct TPlanePoints { - TVector3f m_A; - TVector3f m_B; - TVector3f m_C; + Eigen::Vector3f m_A; + Eigen::Vector3f m_B; + Eigen::Vector3f m_C; std::string m_material; }; -TPlanePoints GetPlanePoints(const TVector3f* _kpPoints, const size_t _kNumPoints) +//-------------------------------------------------------+ +// Component functions of fn 'GetPlanePoints' + +// will need these functions to test whether the outputs used from Eigen +// are the same as the outputs from ckmath + +Eigen::Vector3f get_polygon_normal(const Eigen::Vector3f* vertices, const size_t nvertices) { + Eigen::Vector3f x = {0.0f, 0.0f, 0.0f}; + for (size_t i = 0; i < nvertices; ++i) { + const size_t kIndexA = ((nvertices - 1) + i) % nvertices; + const size_t kIndexB = i; + + const Eigen::Vector3f& krA = vertices[kIndexA]; + const Eigen::Vector3f& krB = vertices[kIndexB]; + + const Eigen::Vector3f kCrossProduct = krA.cross(krB); + x += kCrossProduct; + } + x.normalize(); + return x; +} + +inline Eigen::Vector3f get_center(const Eigen::Vector3f* kppoints, + const size_t npoints) { + Eigen::Vector3f result{0.0f, 0.0f, 0.0f}; + for(size_t i = 0; i < npoints; ++i) + { + result += kppoints[i]; + } + result *= (1.0f / (float)npoints); + return result; +} + +inline float get_width(const Eigen::Vector3f* kppoints, + const size_t npoints, + const Eigen::Vector3f center) { + float width = 1.0f; + for(size_t i = 0; i < npoints; ++i) { + // norm() = vector magnitude in Eigen. + width = std::max(width, (kppoints[i] - center).norm()); + } + return width; +} + +inline Eigen::Vector3f get_tangent(const Eigen::Vector3f* x, + float width) { + Eigen::Vector3f t = (x[1] - x[0]); + t.normalize(); + return (t * (width * 10.0f)); +} + +inline Eigen::Vector3f get_bitangent(const Eigen::Vector3f& tan, + const Eigen::Vector3f& norm, + float width) { + Eigen::Vector3f x = norm.cross(tan); + x.normalize(); + return (x * (width * 10.0f)); +} + +//-------------------------------------------------------+ + +TPlanePoints GetPlanePoints(const Eigen::Vector3f* _kpPoints, const size_t _kNumPoints) { TPlanePoints PlanePoints; + const Eigen::Vector3f kNormal = get_polygon_normal(_kpPoints, _kNumPoints); + const Eigen::Vector3f kCenter = get_center(_kpPoints, _kNumPoints); + float fWidth = get_width(_kpPoints, _kNumPoints, kCenter); + const Eigen::Vector3f kTangent = get_tangent(_kpPoints, fWidth); + const Eigen::Vector3f kBiTangent = get_bitangent(kTangent, kNormal, fWidth); - // Find Normal - const TVector3f kNormal = GetPolygonNormal(TVector3f(), _kpPoints, _kNumPoints); - - // Find center point - TVector3f Temp{0.0f, 0.0f, 0.0f}; - for(size_t i = 0; i < _kNumPoints; ++i) - { - Temp = Add(Temp, Temp, _kpPoints[i]); - } - const TVector3f kCenter = ScalarMultiply(TVector3f(), Temp, (1.0f / (float)_kNumPoints)); - - float fWidth = 1.0f; - for(size_t i = 0; i < _kNumPoints; ++i) - { - fWidth = std::max(fWidth, VectorMagnitude(Subtract(TVector3f(), _kpPoints[i], kCenter))); - } - - // Find Tangent - const TVector3f kTangent = ScalarMultiply(TVector3f(), Normalize(TVector3f(), Subtract(TVector3f(), _kpPoints[1], _kpPoints[0])), fWidth*10.0f); - - // Find BiTangent - const TVector3f kBiTangent = ScalarMultiply(TVector3f(), Normalize(TVector3f(), CrossProduct(TVector3f(), kNormal, kTangent)), fWidth*10.0f); - - PlanePoints.m_A = Add(TVector3f(), kCenter, kBiTangent); + PlanePoints.m_A = kCenter + kBiTangent; PlanePoints.m_B = kCenter; - PlanePoints.m_C = Add(TVector3f(), kCenter, kTangent); + PlanePoints.m_C = kCenter + kTangent; return(PlanePoints); } @@ -56,7 +99,7 @@ std::vector GetBrushPlanes(const TBrush& _krBrush) std::vector Planes; for(const TFace& krFace : _krBrush.m_Faces) { - std::vector Verts(krFace.m_Indices.size()); + std::vector Verts(krFace.m_Indices.size()); for(size_t i = 0; i < krFace.m_Indices.size(); ++i) { Verts[i] = _krBrush.m_Vertices[krFace.m_Indices[i]]; @@ -79,9 +122,9 @@ std::string GetBrushString(const TBrush& _krBrush) ssOutput << "{" << std::endl; for(const TPlanePoints& krPlane : Planes) { - ssOutput << "( " << krPlane.m_A.m_fX << " " << krPlane.m_A.m_fZ << " " << krPlane.m_A.m_fY << " ) "; - ssOutput << "( " << krPlane.m_B.m_fX << " " << krPlane.m_B.m_fZ << " " << krPlane.m_B.m_fY << " ) "; - ssOutput << "( " << krPlane.m_C.m_fX << " " << krPlane.m_C.m_fZ << " " << krPlane.m_C.m_fY << " ) "; + ssOutput << "( " << krPlane.m_A[X] << " " << krPlane.m_A[Z] << " " << krPlane.m_A[Y] << " ) "; + ssOutput << "( " << krPlane.m_B[X] << " " << krPlane.m_B[Z] << " " << krPlane.m_B[Y] << " ) "; + ssOutput << "( " << krPlane.m_C[X] << " " << krPlane.m_C[Z] << " " << krPlane.m_C[Y] << " ) "; if(krPlane.m_material.length()) { ssOutput << krPlane.m_material << " 0 0 0 0.500000 0.500000" << std::endl; @@ -134,4 +177,4 @@ int main(const int _kiArgC, const char** _kppcArgv) } return(0); -} +} \ No newline at end of file