Manta Interactive Ray Tracer Development Mailing List

Text archives Help


[Manta] r1951 - trunk/Model/Primitives


Chronological Thread 
  • From: roni@sci.utah.edu
  • To: manta@sci.utah.edu
  • Subject: [Manta] r1951 - trunk/Model/Primitives
  • Date: Fri, 28 Dec 2007 17:27:23 -0700 (MST)

Author: roni
Date: Fri Dec 28 17:27:21 2007
New Revision: 1951

Added:
   trunk/Model/Primitives/ConvexQuad.cc
   trunk/Model/Primitives/ConvexQuad.h
Modified:
   trunk/Model/Primitives/CMakeLists.txt
Log:
Adding "convex quad" primitive.  Create by passing a material and four
vertices to the constructor.  BadPrimitive is thrown if (a) the
vertices are not co-planar or (b) the polygon is not convex.

This is convex because it makes the intersection test easier.

Primitives/ConvexQuad.cc
Primitives/ConvexQuad.h

  Implementing the primitive.

Primitives/CMakeLists.txt

  Plugging it in.

Modified: trunk/Model/Primitives/CMakeLists.txt
==============================================================================
--- trunk/Model/Primitives/CMakeLists.txt       (original)
+++ trunk/Model/Primitives/CMakeLists.txt       Fri Dec 28 17:27:21 2007
@@ -2,6 +2,8 @@
 SET (Manta_Primitives_SRCS
      Primitives/Cone.cc
      Primitives/Cone.h
+     Primitives/ConvexQuad.cc
+     Primitives/ConvexQuad.h
      Primitives/Cube.cc
      Primitives/Cube.h
      Primitives/Cylinder.cc

Added: trunk/Model/Primitives/ConvexQuad.cc
==============================================================================
--- (empty file)
+++ trunk/Model/Primitives/ConvexQuad.cc        Fri Dec 28 17:27:21 2007
@@ -0,0 +1,99 @@
+#include <Model/Primitives/ConvexQuad.h>
+#include <Core/Exceptions/BadPrimitive.h>
+#include <Core/Geometry/Vector.h>
+#include <Core/Math/MiscMath.h>
+#include <Interface/Context.h>
+#include <Interface/RayPacket.h>
+#include <Model/Primitives/Plane.h>
+
+#include <iostream>
+
+using namespace Manta;
+
+ConvexQuad::ConvexQuad(Material *mat,
+                      const Vector& v0, const Vector& v1, const Vector& v2, 
const Vector& v3)
+  : PrimitiveCommon(mat)
+{
+  // Compute the bounding box.
+  bbox[0] = Min(v0, Min(v1, Min(v2, v3)));
+  bbox[1] = Max(v0, Max(v1, Max(v2, v3)));
+
+  // Store the vertices, as they will be needed in the intersect
+  // routine.
+  v[0] = v0;
+  v[1] = v1;
+  v[2] = v2;
+  v[3] = v3;
+
+  // Create a plane that includes the quad.
+  normal = Cross(v[1]-v[0], v[3]-v[0]);
+  normal.normalize();
+
+  // Clearly, v0, v1, and v3 belong to the plane; check if v2 does too.
+  // TODO: what is the correct value to use instead of 1e-5?
+  if(Abs(Dot(v[2]-v[0], normal)) > 1e-5)
+    throw BadPrimitive("ConvexQuad vertices are not co-planar");
+
+  // Check that the quad is convex.
+  for(int i=0; i<4; i++){
+    int j = (i+1) % 4;
+    int k = (i+2) % 4;
+    if(Dot(Cross(v[j]-v[i], v[k]-v[j]), normal) < 0)
+      throw BadPrimitive("ConvexQuad vertices do not specify a convex 
region");
+  }
+}
+
+ConvexQuad::~ConvexQuad(){
+
+}
+
+void ConvexQuad::computeBounds(const PreprocessContext& context,
+                              BBox& bbox_) const {
+  bbox_.extendByPoint(bbox[0]);
+  bbox_.extendByPoint(bbox[1]);
+}
+
+void ConvexQuad::intersect(const RenderContext& context,
+                          RayPacket& rays) const {
+  const int debugFlag = rays.getAllFlags() & RayPacket::DebugPacket;
+
+  // Begin by intersecting the rays with the plane containing the
+  // quad.
+  for(int i=rays.begin(); i<rays.end(); i++){
+    Real dn = Dot(rays.getDirection(i), normal);
+
+    // The ray is parallel to the plane, so no hit.
+    if(dn == 0)
+      continue;
+
+    Real ao = Dot((v[0]-rays.getOrigin(i)), normal);
+    Real t = ao/dn;
+
+    const Vector p = rays.getOrigin(i) + t*rays.getDirection(i);
+
+    if(debugFlag)
+      cerr << "p = " << p << endl;
+
+    // Now check if the hit position falls inside the convex region
+    // itself.
+    bool inside = true;
+    for(int j=0; j<4; j++){
+      const Real d = Dot(Cross(v[(j+1)%4]-v[j], p-v[j]), normal);
+      if(debugFlag)
+       cerr << "side " << j << ": d = " << d << endl;
+      if(d < 0.0){
+       inside = false;
+       break;
+      }
+    }
+
+    if(inside)
+      rays.hit(i, t, getMaterial(), this, getTexCoordMapper());
+  }
+}
+
+void ConvexQuad::computeNormal(const RenderContext& context,
+                              RayPacket& rays) const {
+  for(int i=rays.begin(); i<rays.end(); i++)
+    rays.setNormal(i, normal);
+}

Added: trunk/Model/Primitives/ConvexQuad.h
==============================================================================
--- (empty file)
+++ trunk/Model/Primitives/ConvexQuad.h Fri Dec 28 17:27:21 2007
@@ -0,0 +1,34 @@
+#ifndef Model_Primitives_ConvexQuad_h
+#define Model_Primitives_ConvexQuad_h
+
+#include <Core/Geometry/BBox.h>
+#include <Model/Primitives/PrimitiveCommon.h>
+
+namespace Manta{
+  class Material;
+  class Plane;
+
+  class ConvexQuad : public PrimitiveCommon {
+  public:
+    ConvexQuad(Material *mat,
+              const Vector& v0, const Vector& v1, const Vector& v2, const 
Vector& v3);
+
+    ~ConvexQuad();
+
+    void computeBounds(const PreprocessContext& context,
+                      BBox& bbox_) const;
+
+    void intersect(const RenderContext& context,
+                  RayPacket& rays) const;
+
+    void computeNormal(const RenderContext& context,
+                      RayPacket& rays) const;
+
+  private:
+    BBox bbox;
+    Vector v[4];
+    Vector normal;
+  };
+}
+
+#endif




  • [Manta] r1951 - trunk/Model/Primitives, roni, 12/28/2007

Archive powered by MHonArc 2.6.16.

Top of page