1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2014 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #ifndef __Ogre_Volume_IsoSurfaceMC_H__
29 #define __Ogre_Volume_IsoSurfaceMC_H__
30 
31 #include "OgreVolumeIsoSurface.h"
32 #include "OgreVector3.h"
33 #include "OgreVector4.h"
34 #include <float.h>
35 
36 namespace Ogre {
37 namespace Volume {
38     /** \addtogroup Optional
39     *  @{
40     */
41     /** \addtogroup Volume
42     *  @{
43     */
44     class Source;
45     class MeshBuilder;
46 
47     /** Marching Cubes implementation like at
48         http://local.wasp.uwa.edu.au/~pbourke/geometry/polygonise/
49      */
50     class _OgreVolumeExport IsoSurfaceMC : public IsoSurface
51     {
52     protected:
53 
54 
55         /** Linear interpolation between two vectors based on some values associated to them.
56         @param v0
57             The first vector.
58         @param v1
59             The second vector.
60         @param val0
61             The value for the first vector.
62         @param val1
63             The value for the second vector.
64         @param normal
65             Reference to a vector where the normal will be stored.
66         @return
67             The interpolated position.
68         */
interpolate(const Vector3 & v0,const Vector3 & v1,const Vector4 & val0,const Vector4 & val1,Vector3 & normal)69         inline Vector3 interpolate(const Vector3 &v0, const Vector3 &v1, const Vector4 &val0, const Vector4 &val1, Vector3 &normal) const
70         {
71             // Don't use Math::RealEqual here as it isn't inlined and this function is performance critical.
72             if (fabs(val0.w - ISO_LEVEL) <= FLT_EPSILON)
73             {
74                 normal.x = val0.x;
75                 normal.y = val0.y;
76                 normal.z = val0.z;
77                 return v0;
78             }
79             if (fabs(val1.w - ISO_LEVEL) <= FLT_EPSILON)
80             {
81                 normal.x = val1.x;
82                 normal.y = val1.y;
83                 normal.z = val1.z;
84                 return v1;
85             }
86             if (fabs(val1.w - val0.w) <= FLT_EPSILON)
87             {
88                 normal.x = val0.x;
89                 normal.y = val0.y;
90                 normal.z = val0.z;
91                 return v0;
92             }
93             Real mu = (ISO_LEVEL - val0.w) / (val1.w - val0.w);
94             Vector4 normal4 = val0 + mu * (val1 - val0);
95             normal.x = normal4.x;
96             normal.y = normal4.y;
97             normal.z = normal4.z;
98             normal.normalise();
99             return v0 + mu * (v1 - v0);
100         }
101 
102     public:
103 
104         /** Constructor.
105         @param src
106             The source for the isovalues and normals there.
107         */
108         explicit IsoSurfaceMC(const Source *src);
109 
110         /** Overridden from IsoSurface.
111         */
112         virtual void addMarchingCubesTriangles(const Vector3 *corners, const Vector4 *volumeValues, MeshBuilder *mb) const;
113 
114         /** Overridden from IsoSurface.
115         */
116         virtual void addMarchingSquaresTriangles(const Vector3 *corners, const Vector4 *volumeValues, const size_t *indices, const Real maxDistance, MeshBuilder *mb) const;
117     };
118     /** @} */
119     /** @} */
120 }
121 }
122 
123 #endif
124