My Project
 All Classes Files Functions Variables Enumerations Pages
triangle_mesh.h
Go to the documentation of this file.
1 /**************************************************************************/
2 /* Copyright 2009 Tim Day */
3 /* */
4 /* This file is part of Fracplanet */
5 /* */
6 /* Fracplanet is free software: you can redistribute it and/or modify */
7 /* it under the terms of the GNU General Public License as published by */
8 /* the Free Software Foundation, either version 3 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /* Fracplanet is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU General Public License */
17 /* along with Fracplanet. If not, see <http://www.gnu.org/licenses/>. */
18 /**************************************************************************/
19 
20 #ifndef _triangle_mesh_h_
21 #define _triangle_mesh_h_
22 
23 #include "geometry.h"
24 #include "parameters_save.h"
25 #include "parameters_terrain.h"
26 #include "progress.h"
27 #include "random.h"
28 #include "triangle.h"
29 #include "triangle_edge.h"
30 #include "vertex.h"
31 
36 
50 {
51 public:
52 
54  TriangleMesh(Progress* progress);
55 
57  virtual ~TriangleMesh();
58 
60  void set_emissive(float e)
61  {
62  _emissive=e;
63  }
64 
66  float emissive() const
67  {
68  return _emissive;
69  }
70 
72  void add_vertex(const Vertex& v)
73  {
74  _vertex.push_back(v);
75  }
76 
78  void add_triangle(const Triangle& t)
79  {
80  _triangle.push_back(t);
81  }
82 
84  const Vertex& vertex(uint i) const
85  {
86  return _vertex[i];
87  }
88 
90  const Triangle& triangle(uint i) const
91  {
92  return _triangle[i];
93  }
94 
96  virtual const Geometry& geometry() const
97  =0;
98 
100  float vertex_height(uint i) const
101  {
102  return geometry().height(vertex(i).position());
103  }
104 
106  void set_vertex_height(uint i,float h)
107  {
108  XYZ p(vertex(i).position());
109  geometry().set_height(p,h);
110  vertex(i).position(p);
111  }
112 
114  float triangle_height_min(uint i) const
115  {
116  const Triangle& t=triangle(i);
117  return minimum
118  (
119  vertex_height(t.vertex(0)),
120  vertex_height(t.vertex(1)),
121  vertex_height(t.vertex(2))
122  );
123  }
124 
126  float triangle_height_max(uint i) const
127  {
128  const Triangle& t=triangle(i);
129  return maximum
130  (
131  vertex_height(t.vertex(0)),
132  vertex_height(t.vertex(1)),
133  vertex_height(t.vertex(2))
134  );
135  }
136 
138  float triangle_height_average(uint i) const
139  {
140  const Triangle& t=triangle(i);
141  return
142  (
143  vertex_height(t.vertex(0))
144  +vertex_height(t.vertex(1))
145  +vertex_height(t.vertex(2))
146  )/3.0;
147  }
148 
150  const XYZ triangle_normal(uint i) const;
151 
153  uint which_colour_for_triangle(uint t) const
154  {
155  return (t<_triangle_switch_colour ? 0 : 1);
156  }
157 
159  uint vertices() const
160  {
161  return _vertex.size();
162  }
164  uint triangles() const
165  {
166  return _triangle.size();
167  }
168 
170  uint triangles_of_colour0() const
171  {
173  }
174 
176  uint triangles_of_colour1() const
177  {
179  }
180 
182  void compute_vertex_normals();
183 
185  void subdivide(const XYZ& variation,uint level,uint levels);
186 
188  void subdivide(uint subdivisions,uint flat_subdivisions,const XYZ& variation);
189 
191  void write_povray(std::ofstream& out,bool exclude_alternate_colour,bool double_illuminate,bool no_shadow) const;
192 
194  void write_blender(std::ofstream& out,const std::string& mesh_name,const FloatRGBA* fake_alpha) const;
195 
196  protected:
197 
199  std::vector<Vertex> _vertex;
200 
202  std::vector<Triangle> _triangle;
203 
206 
208  float _emissive;
209 
212 
214  Vertex& vertex(uint i)
215  {
216  return _vertex[i];
217  }
218 
221  {
222  return _triangle[i];
223  }
224 
226  void progress_start(uint steps,const std::string& info) const;
227 
229  void progress_stall(const std::string& reason) const;
230 
232  void progress_step(uint step) const;
233 
235  void progress_complete(const std::string& info) const;
236 
237  private:
238 
240  static ByteRGBA blender_alpha_workround(const ByteRGBA*,const ByteRGBA&);
241 };
242 
244 class TriangleMeshFlat : virtual public TriangleMesh
245 {
246  public:
247 
249  TriangleMeshFlat(ParametersObject::ObjectType obj,float z,uint seed,Progress* progress);
250 
253  {}
254 
256  virtual const Geometry& geometry() const
257  {
258  return _geometry;
259  }
260 
261  private:
262 
264  GeometryFlat _geometry;
265 };
266 
268 class TriangleMeshIcosahedron : virtual public TriangleMesh
269 {
270  public:
271 
273  TriangleMeshIcosahedron(float radius,uint seed,Progress* progress);
274 
277  {}
278 
280  virtual const Geometry& geometry() const
281  {
282  return _geometry;
283  }
284 
285  private:
286 
288  GeometrySpherical _geometry;
289 };
290 
293 {
294  public:
295 
297  TriangleMeshSubdividedIcosahedron(float radius,uint subdivisions,uint flat_subdivisions,uint seed,const XYZ& variation,Progress* progress);
298 
301  {}
302 };
303 
304 #endif
TriangleMeshFlat(ParametersObject::ObjectType obj, float z, uint seed, Progress *progress)
Constructor.
Definition: triangle_mesh.cpp:385
const Vertex & vertex(uint i) const
Accessor.
Definition: triangle_mesh.h:84
void progress_start(uint steps, const std::string &info) const
Convenience wrapper with null test.
Definition: triangle_mesh.cpp:33
void progress_step(uint step) const
Convenience wrapper with null test.
Definition: triangle_mesh.cpp:41
float vertex_height(uint i) const
Return height of a vertex.
Definition: triangle_mesh.h:100
An icosahedron.
Definition: triangle_mesh.h:268
ObjectType
What kind of object will be generated.
Definition: parameters_object.h:39
void write_povray(std::ofstream &out, bool exclude_alternate_colour, bool double_illuminate, bool no_shadow) const
Dump the mesh to the file in a form suitable for use by POVRay.
Definition: triangle_mesh.cpp:206
std::vector< Triangle > _triangle
The triangles of this mesh.
Definition: triangle_mesh.h:202
Class to represent red-green-blue-alpha colours stored with 8-bit resolution.
Definition: rgb.h:136
float triangle_height_min(uint i) const
Return minimum height of a triangle's vertices.
Definition: triangle_mesh.h:114
float triangle_height_average(uint i) const
Return mean height of a triangle's vertices.
Definition: triangle_mesh.h:138
const Triangle & triangle(uint i) const
Accessor.
Definition: triangle_mesh.h:90
uint _triangle_switch_colour
The index of the triangle at which we switch to the alternate colour.
Definition: triangle_mesh.h:205
Contains vertices and triangles of a triangle mesh.
Definition: triangle_mesh.h:49
float _emissive
The emission level for vertices with the _emissive flag set.
Definition: triangle_mesh.h:208
A subdivided icosahedron.
Definition: triangle_mesh.h:292
TriangleMeshIcosahedron(float radius, uint seed, Progress *progress)
Constructor.
Definition: triangle_mesh.cpp:429
void add_triangle(const Triangle &t)
Append a triangle.
Definition: triangle_mesh.h:78
const XYZ & position() const
Accessor.
Definition: vertex.h:62
virtual const Geometry & geometry() const
Returns the specific geometry.
Definition: triangle_mesh.h:280
const XYZ triangle_normal(uint i) const
Compute and return the normal to a triangle.
Definition: triangle_mesh.cpp:50
void progress_stall(const std::string &reason) const
Convenience wrapper with null test.
Definition: triangle_mesh.cpp:37
Progress *const _progress
Pointer to the progress object to which progress reports should be made.
Definition: triangle_mesh.h:211
void set_emissive(float e)
Accessor.
Definition: triangle_mesh.h:60
void set_vertex_height(uint i, float h)
Set height of a vertex.
Definition: triangle_mesh.h:106
Class to provide abstract interface to different world geometries.
Definition: geometry.h:36
uint which_colour_for_triangle(uint t) const
Return which vertex colour to use for a triangle.
Definition: triangle_mesh.h:153
uint vertices() const
Returns number of vertices in mesh.
Definition: triangle_mesh.h:159
std::vector< Vertex > _vertex
The vertices of this mesh.
Definition: triangle_mesh.h:199
Class to hold vectors in 3D cartesian co-ordinates.
Definition: xyz.h:34
Interface for class Progress.
Mix-in class for call-backs from long operations.
Definition: progress.h:30
Triangle & triangle(uint i)
Accessor.
Definition: triangle_mesh.h:220
void add_vertex(const Vertex &v)
Append a vertex.
Definition: triangle_mesh.h:72
Vertex & vertex(uint i)
Accessor.
Definition: triangle_mesh.h:214
virtual void set_height(XYZ &p, float v) const =0
Move the specified point vertically until.
void write_blender(std::ofstream &out, const std::string &mesh_name, const FloatRGBA *fake_alpha) const
Dump the mesh to the file in a form suitable for use by Blender.
Definition: triangle_mesh.cpp:302
virtual ~TriangleMesh()
Destructor.
Definition: triangle_mesh.cpp:30
float triangle_height_max(uint i) const
Return maximum height of a triangle's vertices.
Definition: triangle_mesh.h:126
uint triangles_of_colour1() const
Returns number of triangles in mesh indexing colour[1] of vertices.
Definition: triangle_mesh.h:176
void compute_vertex_normals()
(Re-)computes vertex normals.
Definition: triangle_mesh.cpp:62
void subdivide(const XYZ &variation, uint level, uint levels)
Perform a single subdivision pass with perturbations up to the specified size.
Definition: triangle_mesh.cpp:103
Interface for class Triangle.
Interface for class Vertex.
~TriangleMeshSubdividedIcosahedron()
Destructor.
Definition: triangle_mesh.h:300
void progress_complete(const std::string &info) const
Convenience wrapper with null test.
Definition: triangle_mesh.cpp:45
virtual const Geometry & geometry() const =0
Access the geometry of this class (needed to abstract concepts like "mid-point" and "height")...
TriangleMesh(Progress *progress)
Constructor.
Definition: triangle_mesh.cpp:24
Class to store triangle state.
Definition: triangle.h:34
~TriangleMeshIcosahedron()
Destructor.
Definition: triangle_mesh.h:276
uint triangles() const
Returns number of triangles in mesh.
Definition: triangle_mesh.h:164
virtual float height(const XYZ &p) const =0
Return the height of the given point.
Concrete class providing a flat geometry (in the XY-plane, with Z up).
Definition: geometry.h:109
Interface for class ParametersSave.
virtual const Geometry & geometry() const
Returns the specific geometry.
Definition: triangle_mesh.h:256
const uint & vertex(uint i) const
Return a reference to the vertex because GL will want it's address.
Definition: triangle.h:56
~TriangleMeshFlat()
Destructor.
Definition: triangle_mesh.h:252
Interface for class TriangleEdge.
Class to represent red-green-blue-alpha colours stored to floating point accuracy.
Definition: rgb.h:173
float emissive() const
Accessor.
Definition: triangle_mesh.h:66
Interface for class Random and derived classes.
uint triangles_of_colour0() const
Returns number of triangles in mesh indexing colour[0] of vertices.
Definition: triangle_mesh.h:170
Interface for class Geometry and derived classes.
TriangleMeshSubdividedIcosahedron(float radius, uint subdivisions, uint flat_subdivisions, uint seed, const XYZ &variation, Progress *progress)
Constructor.
Definition: triangle_mesh.cpp:483
Concrete class providing a flat geometry (a sphere with nominal radius 1, equator in the XY-plane...
Definition: geometry.h:186
Interface for class ParametersTerrain.
Class to store vertex state information.
Definition: vertex.h:35
A single triangle lying in the z-plane.
Definition: triangle_mesh.h:244