Separated planes functions to its own file

This commit is contained in:
2017-04-07 08:19:51 -07:00
parent bc2a3aad8f
commit 19f97c27c1
4 changed files with 170 additions and 136 deletions

View File

@ -5,8 +5,17 @@ CFLAGS=-std=c++11 -I"includes" -I"/usr/include/eigen3"
all: v6 v8 all: v6 v8
v6: v6: planes.o mapparser.o
$(CC) includes/mapparser.cpp main.cpp $(CFLAGS) -o $(EXV6) 2>error6.log $(CC) $^ main.cpp $(CFLAGS) -o $(EXV6) 2>error6.log
v8: v8: planes.o v8mapparser.o
$(CC) includes/v8mapparser.cpp main.cpp $(CFLAGS) -o $(EXV8) 2>error8.log $(CC) $^ main.cpp $(CFLAGS) -o $(EXV8) 2>error8.log
mapparser.o:
$(CC) -c includes/mapparser.cpp $(CFLAGS)
v8mapparser.o:
$(CC) -c includes/v8mapparser.cpp $(CFLAGS)
planes.o:
$(CC) -c includes/planes.cpp $(CFLAGS)

View File

@ -0,0 +1,125 @@
#include "libraries.h"
#include "planes.h"
//-------------------------------------------------------+
// 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);
PlanePoints.m_A = kCenter + kBiTangent;
PlanePoints.m_B = kCenter;
PlanePoints.m_C = kCenter + kTangent;
return(PlanePoints);
}
std::vector<TPlanePoints> GetBrushPlanes(const TBrush& _krBrush)
{
std::vector<TPlanePoints> Planes;
for(const TFace& krFace : _krBrush.m_Faces)
{
std::vector<Eigen::Vector3f> 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]];
}
TPlanePoints PP = GetPlanePoints(Verts.data(), Verts.size());
PP.m_material = krFace.m_Material;
Planes.push_back(PP);
}
return(Planes);
}
std::string GetBrushString(const TBrush& _krBrush)
{
std::vector<TPlanePoints> Planes = GetBrushPlanes(_krBrush);
std::stringstream ssOutput;
if(Planes.size())
{
ssOutput << "{" << std::endl;
for(const TPlanePoints& krPlane : Planes)
{
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;
}
else
{
ssOutput << "common/caulk 0 0 0 0.500000 0.500000" << std::endl;
}
}
ssOutput << "}" << std::endl;
}
return(ssOutput.str());
}

View File

@ -0,0 +1,31 @@
#include "worldspawn.h"
#include <Eigen/Core>
#include <Eigen/Geometry>
#ifndef __PLANES_H__
#define __PLANES_H__
struct TPlanePoints {
Eigen::Vector3f m_A;
Eigen::Vector3f m_B;
Eigen::Vector3f m_C;
std::string m_material;
};
Eigen::Vector3f get_polygon_normal(const Eigen::Vector3f*, const size_t);
Eigen::Vector3f get_center(const Eigen::Vector3f*, const size_t);
float get_width(const Eigen::Vector3f*, const size_t, const Eigen::Vector3f);
Eigen::Vector3f get_tangent(const Eigen::Vector3f*, float);
Eigen::Vector3f get_bitangent(const Eigen::Vector3f&, const Eigen::Vector3f&, float);
TPlanePoints GetPlanePoints(const Eigen::Vector3f*, const size_t);
std::vector<TPlanePoints> GetBrushPlanes(const TBrush&);
std::string GetBrushString(const TBrush&);
#endif

View File

@ -7,138 +7,7 @@
#include "libraries.h" #include "libraries.h"
// Local Includes // Local Includes
#include "mapparser.h" #include "mapparser.h"
#include "planes.h"
// Structs
struct TPlanePoints
{
Eigen::Vector3f m_A;
Eigen::Vector3f m_B;
Eigen::Vector3f m_C;
std::string m_material;
};
//-------------------------------------------------------+
// 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);
PlanePoints.m_A = kCenter + kBiTangent;
PlanePoints.m_B = kCenter;
PlanePoints.m_C = kCenter + kTangent;
return(PlanePoints);
}
std::vector<TPlanePoints> GetBrushPlanes(const TBrush& _krBrush)
{
std::vector<TPlanePoints> Planes;
for(const TFace& krFace : _krBrush.m_Faces)
{
std::vector<Eigen::Vector3f> 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]];
}
TPlanePoints PP = GetPlanePoints(Verts.data(), Verts.size());
PP.m_material = krFace.m_Material;
Planes.push_back(PP);
}
return(Planes);
}
std::string GetBrushString(const TBrush& _krBrush)
{
std::vector<TPlanePoints> Planes = GetBrushPlanes(_krBrush);
std::stringstream ssOutput;
if(Planes.size())
{
ssOutput << "{" << std::endl;
for(const TPlanePoints& krPlane : Planes)
{
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;
}
else
{
ssOutput << "common/caulk 0 0 0 0.500000 0.500000" << std::endl;
}
}
ssOutput << "}" << std::endl;
}
return(ssOutput.str());
}
int main(const int _kiArgC, const char** _kppcArgv) int main(const int _kiArgC, const char** _kppcArgv)
{ {