moved from ray3 to floorplan/3D
worked on new wall models refactoring
This commit is contained in:
166
floorplan/3D/primitives/Cube.h
Normal file
166
floorplan/3D/primitives/Cube.h
Normal file
@@ -0,0 +1,166 @@
|
||||
#ifndef FLOORPLAN_3D_CUBE_H
|
||||
#define FLOORPLAN_3D_CUBE_H
|
||||
|
||||
|
||||
#include "../../../math/Matrix4.h"
|
||||
#include "Mesh.h"
|
||||
|
||||
namespace Floorplan3D {
|
||||
|
||||
class Cube : public Mesh {
|
||||
|
||||
private:
|
||||
|
||||
/** ctor */
|
||||
Cube() {
|
||||
//unitCube(true);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
|
||||
enum Part {
|
||||
CUBE_TOP = 1,
|
||||
CUBE_BOTTOM = 2,
|
||||
CUBE_LEFT = 4,
|
||||
CUBE_RIGHT = 8,
|
||||
CUBE_FRONT = 16,
|
||||
CUBE_BACK = 32,
|
||||
};
|
||||
|
||||
// Cube (const Point3 p1, const Point3 p2, const Point3 p3, const Point3 p4, const float h) {
|
||||
//// const Point3 ph(0,0,h);
|
||||
//// addQuad(p1+ph, p2+ph, p3+ph, p4+ph); // top
|
||||
//// addQuad(p4, p3, p2, p1); // bottom
|
||||
//// addQuad(p3+ph, p2+ph, p2, p3); // right
|
||||
//// addQuad(p1+ph, p4+ph, p4, p1); // left
|
||||
//// addQuad(p2+ph, p1+ph, p1, p2); // front
|
||||
//// addQuad(p4+ph, p3+ph, p3, p4); // back
|
||||
// addQuad();
|
||||
// }
|
||||
|
||||
/** ctor with position, size and rotation */
|
||||
Cube(const Point3 pos, const Point3 size, const Point3 rot_deg, const Part parts = (Part)63) {
|
||||
unitCube(parts);
|
||||
transform(pos, size, rot_deg);
|
||||
}
|
||||
|
||||
|
||||
/** get a transformed version */
|
||||
Cube transformed(const Matrix4& mat) const {
|
||||
Cube res = *this;
|
||||
res.transform(mat);
|
||||
return res;
|
||||
}
|
||||
|
||||
/** get a transformed version */
|
||||
Cube transformed(const Point3 pos, const Point3 size, const Point3 rot_deg) const {
|
||||
Cube res = *this;
|
||||
res.transform(pos, size, rot_deg);
|
||||
return res;
|
||||
}
|
||||
|
||||
static Cube unit(const Part parts = (Part) 63) {
|
||||
Cube cube;
|
||||
cube.unitCube(parts);
|
||||
return cube;
|
||||
}
|
||||
|
||||
/** cube from 8 vertices (upper 4, lower 4) */
|
||||
static Cube fromVertices(const Point3 pt1, const Point3 pt2, const Point3 pt3, const Point3 pt4, const Point3 pb1, const Point3 pb2, const Point3 pb3, const Point3 pb4) {
|
||||
Cube cube;
|
||||
cube.addQuad(pt1, pt2, pt3, pt4); // top
|
||||
cube.addQuad(pb4, pb3, pb2, pb1); // bottom
|
||||
cube.addQuad(pt3, pt2, pb2, pb3); // right
|
||||
cube.addQuad(pt1, pt4, pb4, pb1); // left
|
||||
cube.addQuad(pt2, pt1, pb1, pb2); // front
|
||||
cube.addQuad(pt4, pt3, pb3, pb4); // back
|
||||
return cube;
|
||||
}
|
||||
|
||||
/** cube from 8 vertices (upper 4, lower 4) */
|
||||
static Cube fromBottomAndHeight(const Point3 pb1, const Point3 pb2, const Point3 pb3, const Point3 pb4, const float h) {
|
||||
const Point3 ph(0,0,h);
|
||||
return Cube::fromVertices(pb1+ph, pb2+ph, pb3+ph, pb4+ph, pb1, pb2, pb3, pb4);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
/** build unit-cube faces */
|
||||
void unitCube(const Part parts) {
|
||||
|
||||
const float s = 1.0f;
|
||||
|
||||
|
||||
|
||||
// left?
|
||||
if (parts & CUBE_LEFT) {
|
||||
addQuad(
|
||||
Point3(+s, -s, -s),
|
||||
Point3(+s, -s, +s),
|
||||
Point3(-s, -s, +s),
|
||||
Point3(-s, -s, -s)
|
||||
);
|
||||
}
|
||||
|
||||
// right?
|
||||
if (parts & CUBE_RIGHT) {
|
||||
addQuad(
|
||||
Point3(-s, +s, -s),
|
||||
Point3(-s, +s, +s),
|
||||
Point3(+s, +s, +s),
|
||||
Point3(+s, +s, -s)
|
||||
);
|
||||
}
|
||||
|
||||
// front
|
||||
if (parts & CUBE_FRONT) {
|
||||
addQuad(
|
||||
Point3(-s, -s, -s),
|
||||
Point3(-s, -s, +s),
|
||||
Point3(-s, +s, +s),
|
||||
Point3(-s, +s, -s)
|
||||
);
|
||||
}
|
||||
|
||||
// back
|
||||
if (parts & CUBE_BACK) {
|
||||
addQuad(
|
||||
Point3(+s, +s, -s),
|
||||
Point3(+s, +s, +s),
|
||||
Point3(+s, -s, +s),
|
||||
Point3(+s, -s, -s)
|
||||
);
|
||||
}
|
||||
|
||||
// top
|
||||
if (parts & CUBE_TOP) {
|
||||
addQuad(
|
||||
Point3(+s, +s, +s),
|
||||
Point3(-s, +s, +s),
|
||||
Point3(-s, -s, +s),
|
||||
Point3(+s, -s, +s)
|
||||
);
|
||||
}
|
||||
|
||||
// bottom
|
||||
if (parts & CUBE_BOTTOM) {
|
||||
addQuad(
|
||||
Point3(+s, -s, -s),
|
||||
Point3(-s, -s, -s),
|
||||
Point3(-s, +s, -s),
|
||||
Point3(+s, +s, -s)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // FLOORPLAN_3D_CUBE_H
|
||||
82
floorplan/3D/primitives/Cylinder.h
Normal file
82
floorplan/3D/primitives/Cylinder.h
Normal file
@@ -0,0 +1,82 @@
|
||||
#ifndef FLOORPLAN_3D_CYLINDER_H
|
||||
#define FLOORPLAN_3D_CYLINDER_H
|
||||
|
||||
|
||||
#include "../../../math/Matrix4.h"
|
||||
#include "Mesh.h"
|
||||
|
||||
namespace Floorplan3D {
|
||||
|
||||
/** walled cylinder */
|
||||
class Cylinder : public Mesh {
|
||||
|
||||
public:
|
||||
|
||||
/** ctor */
|
||||
Cylinder() {
|
||||
;
|
||||
}
|
||||
|
||||
/** get a transformed version */
|
||||
Cylinder transformed(const Matrix4& mat) const {
|
||||
Cylinder res = *this;
|
||||
res.transform(mat);
|
||||
return res;
|
||||
}
|
||||
|
||||
/** build */
|
||||
void add(const float rOuter, const float h, bool topAndBottom) {
|
||||
|
||||
const int tiles = 8;
|
||||
const float deg_per_tile = 360.0f / tiles;
|
||||
const float rad_per_tile = deg_per_tile / 180.0f * M_PI;
|
||||
|
||||
for (int i = 0; i < tiles; ++i) {
|
||||
|
||||
const float startRad = (i+0) * rad_per_tile;
|
||||
const float endRad = (i+1) * rad_per_tile;
|
||||
|
||||
const float xo0 = std::cos(startRad) * rOuter;
|
||||
const float yo0 = std::sin(startRad) * rOuter;
|
||||
const float xo1 = std::cos(endRad) * rOuter;
|
||||
const float yo1 = std::sin(endRad) * rOuter;
|
||||
const float cx = 0;
|
||||
const float cy = 0;
|
||||
|
||||
// outer
|
||||
addQuad(
|
||||
Point3(xo0, yo0, -h),
|
||||
Point3(xo1, yo1, -h),
|
||||
Point3(xo1, yo1, +h),
|
||||
Point3(xo0, yo0, +h)
|
||||
);
|
||||
|
||||
if (topAndBottom) {
|
||||
|
||||
// top
|
||||
addTriangle(
|
||||
Point3(cx, cy, h),
|
||||
Point3(xo0, yo0, h),
|
||||
Point3(xo1, yo1, h)
|
||||
);
|
||||
|
||||
// bottom
|
||||
addTriangle(
|
||||
Point3(cx, cy, -h),
|
||||
Point3(xo1, yo1, -h),
|
||||
Point3(xo0, yo0, -h)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // FLOORPLAN_3D_CYLINDER_H
|
||||
69
floorplan/3D/primitives/Mesh.h
Normal file
69
floorplan/3D/primitives/Mesh.h
Normal file
@@ -0,0 +1,69 @@
|
||||
#ifndef FLOORPLAN_3D_MESH_H
|
||||
#define FLOORPLAN_3D_MESH_H
|
||||
|
||||
#include <vector>
|
||||
#include "../../../geo/Triangle3.h"
|
||||
#include "../../../math/Matrix4.h"
|
||||
|
||||
namespace Floorplan3D {
|
||||
|
||||
class Mesh {
|
||||
|
||||
protected:
|
||||
|
||||
std::vector<Triangle3> trias;
|
||||
|
||||
public:
|
||||
|
||||
/** get the mesh's triangles */
|
||||
const std::vector<Triangle3>& getTriangles() const {
|
||||
return trias;
|
||||
}
|
||||
|
||||
void transform(const Point3 pos, Point3 size, Point3 rot_deg) {
|
||||
const Matrix4 mRot = Matrix4::getRotationDeg(rot_deg.x, rot_deg.y, rot_deg.z);
|
||||
const Matrix4 mSize = Matrix4::getScale(size.x, size.y, size.z);
|
||||
const Matrix4 mPos = Matrix4::getTranslation(pos.x, pos.y, pos.z);
|
||||
const Matrix4 mat = mPos * mRot * mSize;
|
||||
transform(mat);
|
||||
}
|
||||
|
||||
void translate(const Point3 pos) {
|
||||
const Matrix4 mPos = Matrix4::getTranslation(pos.x, pos.y, pos.z);
|
||||
transform(mPos);
|
||||
}
|
||||
|
||||
void transform(const Matrix4& mat) {
|
||||
|
||||
for (Triangle3& tria : trias) {
|
||||
|
||||
Vector4 v1(tria.p1.x, tria.p1.y, tria.p1.z, 1);
|
||||
Vector4 v2(tria.p2.x, tria.p2.y, tria.p2.z, 1);
|
||||
Vector4 v3(tria.p3.x, tria.p3.y, tria.p3.z, 1);
|
||||
|
||||
v1 = mat*v1;
|
||||
v2 = mat*v2;
|
||||
v3 = mat*v3;
|
||||
|
||||
tria.p1 = Point3(v1.x, v1.y, v1.z);
|
||||
tria.p2 = Point3(v2.x, v2.y, v2.z);
|
||||
tria.p3 = Point3(v3.x, v3.y, v3.z);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void addQuad(Point3 p1, Point3 p2, Point3 p3, Point3 p4) {
|
||||
trias.push_back( Triangle3(p1,p2,p3) );
|
||||
trias.push_back( Triangle3(p1,p3,p4) );
|
||||
}
|
||||
|
||||
void addTriangle(Point3 p1, Point3 p2, Point3 p3) {
|
||||
trias.push_back( Triangle3(p1,p2,p3) );
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // FLOORPLAN_3D_MESH_H
|
||||
122
floorplan/3D/primitives/Tube.h
Normal file
122
floorplan/3D/primitives/Tube.h
Normal file
@@ -0,0 +1,122 @@
|
||||
#ifndef FLOORPLAN_3D_TUBE_H
|
||||
#define FLOORPLAN_3D_TUBE_H
|
||||
|
||||
|
||||
#include "../../../math/Matrix4.h"
|
||||
#include "Mesh.h"
|
||||
|
||||
namespace Floorplan3D {
|
||||
|
||||
/** walled cylinder */
|
||||
class Tube : public Mesh {
|
||||
|
||||
public:
|
||||
|
||||
/** ctor */
|
||||
Tube() {
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
/** get a transformed version */
|
||||
Tube transformed(const Matrix4& mat) const {
|
||||
Tube res = *this;
|
||||
res.transform(mat);
|
||||
return res;
|
||||
}
|
||||
|
||||
/** build */
|
||||
void addSegment(const float from_deg, const float to_deg, const float rInner, const float rOuter, const float h, bool closeSides, bool topAndBottom) {
|
||||
|
||||
const int tiles = 32;
|
||||
const float deg_per_tile = 360.0f / tiles;
|
||||
const float rad_per_tile = deg_per_tile / 180.0f * M_PI;
|
||||
const int startTile = std::round(from_deg / deg_per_tile);
|
||||
const int endTile = std::round(to_deg / deg_per_tile);
|
||||
|
||||
for (int i = startTile; i < endTile; ++i) {
|
||||
|
||||
const float startRad = (i+0) * rad_per_tile;
|
||||
const float endRad = (i+1) * rad_per_tile;
|
||||
|
||||
const float xo0 = std::cos(startRad) * rOuter;
|
||||
const float yo0 = std::sin(startRad) * rOuter;
|
||||
const float xo1 = std::cos(endRad) * rOuter;
|
||||
const float yo1 = std::sin(endRad) * rOuter;
|
||||
|
||||
const float xi0 = std::cos(startRad) * rInner;
|
||||
const float yi0 = std::sin(startRad) * rInner;
|
||||
const float xi1 = std::cos(endRad) * rInner;
|
||||
const float yi1 = std::sin(endRad) * rInner;
|
||||
|
||||
if (closeSides) {
|
||||
|
||||
// close start of segment
|
||||
if (i == startTile) {
|
||||
addQuad(
|
||||
Point3(xi0, yi0, -h),
|
||||
Point3(xo0, yo0, -h),
|
||||
Point3(xo0, yo0, +h),
|
||||
Point3(xi0, yi0, +h)
|
||||
);
|
||||
}
|
||||
|
||||
// close end of segment
|
||||
if (i == endTile-1) {
|
||||
addQuad(
|
||||
Point3(xi1, yi1, +h),
|
||||
Point3(xo1, yo1, +h),
|
||||
Point3(xo1, yo1, -h),
|
||||
Point3(xi1, yi1, -h)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// outer
|
||||
addQuad(
|
||||
Point3(xo0, yo0, -h),
|
||||
Point3(xo1, yo1, -h),
|
||||
Point3(xo1, yo1, +h),
|
||||
Point3(xo0, yo0, +h)
|
||||
);
|
||||
|
||||
// innser
|
||||
addQuad(
|
||||
Point3(xi0, yi0, +h),
|
||||
Point3(xi1, yi1, +h),
|
||||
Point3(xi1, yi1, -h),
|
||||
Point3(xi0, yi0, -h)
|
||||
);
|
||||
|
||||
if (topAndBottom) {
|
||||
|
||||
// top
|
||||
addQuad(
|
||||
Point3(xi0, yi0, h),
|
||||
Point3(xo0, yo0, h),
|
||||
Point3(xo1, yo1, h),
|
||||
Point3(xi1, yi1, h)
|
||||
);
|
||||
|
||||
// bottom
|
||||
addQuad(
|
||||
Point3(xi1, yi1, -h),
|
||||
Point3(xo1, yo1, -h),
|
||||
Point3(xo0, yo0, -h),
|
||||
Point3(xi0, yi0, -h)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // FLOORPLAN_3D_TUBE_H
|
||||
Reference in New Issue
Block a user