NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ConvexPolyhedron.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2011, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 #include "3d/Mesh.hpp"
30 
31 namespace FW
32 {
33 //------------------------------------------------------------------------
34 
36 {
37 public:
38  struct Vertex
39  {
41  union
42  {
45  } aux;
46  };
47 
48  struct Edge
49  {
52  };
53 
54  struct Face
55  {
61  };
62 
63  struct FaceEdge
64  {
67  };
68 
69 public:
70  ConvexPolyhedron (void) {}
71  ConvexPolyhedron (const Vec3f& lo, const Vec3f& hi) { setCube(lo, hi); }
72  ConvexPolyhedron (const ConvexPolyhedron& other) { set(other); }
74 
75  int getNumVertices (void) const { return m_vertices.getSize(); }
76  const Vec3f& getVertex (int idx) const { return m_vertices[idx].pos; }
77 
78  int getNumEdges (void) const { return m_edges.getSize(); }
79  Vec2i getEdge (int idx) const { const Vec2i& e = m_edges[idx ^ (idx >> 31)].verts; return (idx >= 0) ? e : Vec2i(e.y, e.x); }
80  int getEdgeStartVertex (int idx) const { return (idx >= 0) ? m_edges[idx].verts.x : m_edges[~idx].verts.y; }
81  int getEdgeEndVertex (int idx) const { return (idx >= 0) ? m_edges[idx].verts.y : m_edges[~idx].verts.x; }
82  const Vec3f& getEdgeStartPos (int idx) const { return m_vertices[getEdgeStartVertex(idx)].pos; }
83  const Vec3f& getEdgeEndPos (int idx) const { return m_vertices[getEdgeEndVertex(idx)].pos; }
84 
85  int getNumFaces (void) const { return m_faces.getSize(); }
86  const Vec4f& getFacePlaneEq (int idx) const { return m_faces[idx].planeEq; }
87  int getFacePlaneID (int idx) const { return m_faces[idx].planeID; }
88  int getFaceNumEdges (int idx) const { return m_faces[idx].numEdges; }
89  int getFaceEdge (int faceIdx, int idx) { FW_ASSERT(idx >= 0 && idx < m_faces[faceIdx].numEdges); return m_faceEdges[m_faces[faceIdx].firstEdge + idx].edge; }
90 
91  void set (const ConvexPolyhedron& other) { m_vertices = other.m_vertices; m_edges = other.m_edges; m_faces = other.m_faces; m_faceEdges = other.m_faceEdges; }
92  void setEmpty (void) { m_vertices.clear(); m_edges.clear(); m_faces.clear(); m_faceEdges.clear(); }
93  void setCube (const Vec3f& lo, const Vec3f& hi);
94 
95  bool intersect (const Vec4f& planeEq, int planeID = -1);
96  bool intersect (const ConvexPolyhedron& other);
97  bool intersectCube (const Vec3f& lo, const Vec3f& hi) { ConvexPolyhedron t; t.setCube(lo, hi); return intersect(t); }
98 
99  F32 computeVolume (void) const;
100  F32 computeArea (int faceIdx) const;
101  F32 computeArea (void) const { F32 t = 0.0f; for (int i = 0; i < m_faces.getSize(); i++) t += computeArea(i); return t; }
102  Vec3f computeCenterOfMass (int faceIdx) const;
103  Vec3f computeCenterOfMass (void) const;
104  Mesh<VertexPN>* createMesh (void) const;
105 
106  ConvexPolyhedron& operator= (const ConvexPolyhedron& other) { set(other); return *this; }
107 
108 private:
109  Array<Vertex> m_vertices;
110  Array<Edge> m_edges;
111  Array<Face> m_faces;
112  Array<FaceEdge> m_faceEdges;
113 };
114 
115 //------------------------------------------------------------------------
116 }
const Vec3f & getVertex(int idx) const
void set(const ConvexPolyhedron &other)
int getEdgeEndVertex(int idx) const
Mesh< VertexPN > * createMesh(void) const
int getEdgeStartVertex(int idx) const
ConvexPolyhedron(const Vec3f &lo, const Vec3f &hi)
int getNumFaces(void) const
bool intersect(const Vec4f &planeEq, int planeID=-1)
int getNumEdges(void) const
ConvexPolyhedron & operator=(const ConvexPolyhedron &other)
float F32
Definition: Defs.hpp:89
ConvexPolyhedron(const ConvexPolyhedron &other)
const Vec3f & getEdgeEndPos(int idx) const
#define FW_ASSERT(X)
Definition: Defs.hpp:67
signed int S32
Definition: Defs.hpp:88
int getFacePlaneID(int idx) const
void setCube(const Vec3f &lo, const Vec3f &hi)
bool intersectCube(const Vec3f &lo, const Vec3f &hi)
union FW::ConvexPolyhedron::Vertex::@0 aux
F32 computeArea(void) const
int getFaceNumEdges(int idx) const
const Vec4f & getFacePlaneEq(int idx) const
int getFaceEdge(int faceIdx, int idx)
Vec3f computeCenterOfMass(void) const
int getNumVertices(void) const
Vec2i getEdge(int idx) const
F32 computeVolume(void) const
const Vec3f & getEdgeStartPos(int idx) const