diff --git a/exercise4/include/AABBTree.h b/exercise4/include/AABBTree.h index 6e6e893..38f3dc8 100644 --- a/exercise4/include/AABBTree.h +++ b/exercise4/include/AABBTree.h @@ -1,5 +1,5 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved #pragma once @@ -13,31 +13,33 @@ #include "LineSegment.h" #include "Point.h" - /** * Axis aligned bounding volume hierachy data structure. */ template class AABBTree { -public: + public: typedef std::vector primitive_list; //iterator type pointing inside the primitive list typedef typename primitive_list::iterator PrimitiveIterator; //const iterator type pointing inside the primitive list typedef typename primitive_list::const_iterator const_primitive_iterator; - + //abstract base class defining the common interface of all aabb tree node class AABBNode { - protected: + protected: //storage of bounding box assosiated with aabb_node Box bounds; - public: - AABBNode() { + + public: + AABBNode() + { } - AABBNode(const Box& b): bounds(b) { + AABBNode(const Box &b) : bounds(b) + { } //returns the bounding box of the node @@ -47,28 +49,24 @@ public: } virtual int NumPrimitives() const = 0; - //this method must be implemented to return true for a leaf node and false for a non_lef node virtual bool IsLeaf() const = 0; //virtual destructor virtual ~AABBNode() {} - }; ///a class representing a leaf node of an aabb tree (non split node) - class AABBLeafNode: public AABBNode + class AABBLeafNode : public AABBNode { - + //internal storage to the range (begin and end pointer) of the primitives associated with the current leaf node PrimitiveIterator primitivesBegin, primitivesEnd; - public: - - - //construct a leaf node from - AABBLeafNode(const PrimitiveIterator& primitivesBegin, - const PrimitiveIterator& primitivesEnd, - const Box& b): - primitivesBegin(primitivesBegin),primitivesEnd(primitivesEnd), AABBNode(b) + + public: + //construct a leaf node from + AABBLeafNode(const PrimitiveIterator &primitivesBegin, + const PrimitiveIterator &primitivesEnd, + const Box &b) : primitivesBegin(primitivesBegin), primitivesEnd(primitivesEnd), AABBNode(b) { } @@ -80,7 +78,7 @@ public: //returns the number primitives assosiated with the current leaf int NumPrimitives() const { - return (int)(primitivesEnd-primitivesBegin); + return (int)(primitivesEnd - primitivesBegin); } const_primitive_iterator begin() const @@ -92,23 +90,22 @@ public: { return primitivesEnd; } - - }; ///a class representing a split node of an aabb tree (non leaf node) - class AABBSplitNode: public AABBNode + class AABBSplitNode : public AABBNode { //child pointers - AABBNode* children[2]; - public: + AABBNode *children[2]; + + public: //default constructor AABBSplitNode() { children[0] = children[1] = nullptr; } //construct a split node from given Left and right child pointers and given bounding box b of the node - AABBSplitNode(AABBNode* left_child, AABBNode* right_child,const Box& b): AABBNode(b) + AABBSplitNode(AABBNode *left_child, AABBNode *right_child, const Box &b) : AABBNode(b) { children[0] = left_child; children[1] = right_child; @@ -117,9 +114,9 @@ public: //destructor of node, recursively deleted whole subtree ~AABBSplitNode() { - if(Left() != nullptr) + if (Left() != nullptr) delete Left(); - if(Right() != nullptr) + if (Right() != nullptr) delete Right(); } @@ -128,26 +125,26 @@ public: { return false; } - + //returns a pointer to the left child node - AABBNode* Left() + AABBNode *Left() { return children[0]; } //returns a pointer to the right child node - AABBNode* Right() + AABBNode *Right() { return children[1]; } - //returns a const pointer to the left child node - const AABBNode* Left() const + //returns a const pointer to the left child node + const AABBNode *Left() const { return children[0]; } - //returns a const pointer to the right child node - const AABBNode* Right() const + //returns a const pointer to the right child node + const AABBNode *Right() const { return children[1]; } @@ -155,27 +152,27 @@ public: //counts the number of primitives of the subtree int NumPrimitives() const { - return Left()->NumPrimitives() + Right()->NumPrimitives(); - } - + return Left()->NumPrimitives() + Right()->NumPrimitives(); + } }; -private: + private: //search entry used internally for nearest and k nearest primitive queries struct SearchEntry { //squared distance to node from query point float sqrDistance; //node - const AABBNode* node; - + const AABBNode *node; + //constructor - SearchEntry(float sqrDistance, const AABBNode* node) + SearchEntry(float sqrDistance, const AABBNode *node) : sqrDistance(sqrDistance), node(node) - { } - - //search entry a < b means a.sqr_distance > b. sqr_distance - bool operator<(const SearchEntry& e) const + { + } + + //search entry a < b means a.sqr_distance > b. sqr_distance + bool operator<(const SearchEntry &e) const { return sqrDistance > e.sqrDistance; } @@ -187,17 +184,19 @@ private: //squared distance from query point to primitive float sqrDistance; //pointer to primitive - const Primitive* prim; + const Primitive *prim; //default constructor ResultEntry() : sqrDistance(std::numeric_limits::infinity()), prim(nullptr) - { } + { + } //constructor - ResultEntry(float sqrDistance, const Primitive* p) + ResultEntry(float sqrDistance, const Primitive *p) : sqrDistance(sqrDistance), prim(p) - { } - //result_entry are sorted by their sqr_distance using this less than operator - bool operator<(const ResultEntry& e) const + { + } + //result_entry are sorted by their sqr_distance using this less than operator + bool operator<(const ResultEntry &e) const { return sqrDistance < e.sqrDistance; } @@ -214,72 +213,69 @@ private: //a flag indicating if the tree is constructed bool completed; - -public: + public: //returns a pointer to the root node of the tree - AABBNode* Root() + AABBNode *Root() { assert(IsCompleted()); return root; } //returns a const pointer to the root node of the tree - const AABBNode* Root() const + const AABBNode *Root() const { assert(IsCompleted()); return root; } - //constructor of aabb tree - //default maximal tree depth is 20 - //default minimal size of a node not to be further subdivided in the cnstruction process is two - AABBTree(int maxDepth=20, int minSize=2): - maxDepth(maxDepth),minSize(minSize),root(nullptr),completed(false) + //constructor of aabb tree + //default maximal tree depth is 20 + //default minimal size of a node not to be further subdivided in the cnstruction process is two + AABBTree(int maxDepth = 20, int minSize = 2) : maxDepth(maxDepth), minSize(minSize), root(nullptr), completed(false) { - } //copy constructor - AABBTree(const AABBTree& other) + AABBTree(const AABBTree &other) { primitives = other.primitives; maxDepth = other.maxDepth; minSize = other.minSize; - root = CopyTree(other.primitives,other.root); + root = CopyTree(other.primitives, other.root); completed = other.completed; } - + //move constructor - AABBTree(AABBTree&& other):root(nullptr),completed(false) + AABBTree(AABBTree &&other) : root(nullptr), completed(false) { *this = std::move(other); } //copy assignment operator - AABBTree& operator=(const AABBTree& other) + AABBTree &operator=(const AABBTree &other) { - if(this != &other) + if (this != &other) { - if(root != nullptr) + if (root != nullptr) delete root; primitives = other.primitives; maxDepth = other.maxDepth; minSize = other.minSize; - root = CopyTree(other.primitives,other.root); + root = CopyTree(other.primitives, other.root); completed = other.completed; } return *this; } //move assign operator - AABBTree& operator=(AABBTree&& other) + AABBTree &operator=(AABBTree &&other) { - if(this != &other) - { - std::swap(primitives,other.primitives); + if (this != &other) + { + std::swap(primitives, other.primitives); std::swap(maxDepth, other.maxDepth); std::swap(minSize, other.minSize); - std::swap(root,other.root) ; + std::swap(root, other.root); std::swap(completed, other.completed); } return *this; @@ -289,7 +285,7 @@ public: void Clear() { primitives.clear(); - if(root != nullptr) + if (root != nullptr) { delete root; root = nullptr; @@ -302,28 +298,28 @@ public: { return primitives.Empty(); } - - //insert a primitive into internal primitive list + + //insert a primitive into internal primitive list //this method do not construct the tree! //call the method Complete, after insertion of all primitives - void Insert(const Primitive& p) + void Insert(const Primitive &p) { primitives.push_back(p); - completed=false; + completed = false; } - - //construct the tree from all prior inserted primitives + + //construct the tree from all prior inserted primitives void Complete() { //if tree already constructed -> delete tree - if(root != nullptr) + if (root != nullptr) delete root; //compute bounding box over all primitives using helper function - Box bounds = ComputeBounds(primitives.begin(),primitives.end()); + Box bounds = ComputeBounds(primitives.begin(), primitives.end()); //initial call to the recursive tree construction method over the whole range of primitives - root = Build(primitives.begin(),primitives.end(),bounds,0); + root = Build(primitives.begin(), primitives.end(), bounds, 0); //set completed flag to true - completed=true; + completed = true; } //returns true if the tree can be used for queries @@ -334,15 +330,15 @@ public: } //closest primitive computation via linear search - ResultEntry ClosestPrimitiveLinearSearch(const Eigen::Vector3f& q) const + ResultEntry ClosestPrimitiveLinearSearch(const Eigen::Vector3f &q) const { ResultEntry best; - + auto pend = primitives.end(); - for(auto pit = primitives.begin(); pit != pend; ++pit) + for (auto pit = primitives.begin(); pit != pend; ++pit) { float dist = pit->SqrDistance(q); - if(dist < best.sqrDistance) + if (dist < best.sqrDistance) { best.sqrDistance = dist; best.prim = &(*pit); @@ -352,162 +348,153 @@ public: } //computes the k nearest neighbor primitives via linear search - std::vector ClosestKPrimitivesLinearSearch(size_t k, const Eigen::Vector3f& q) const + std::vector ClosestKPrimitivesLinearSearch(size_t k, const Eigen::Vector3f &q) const { std::priority_queue k_best; - + Primitive best_p; auto pend = primitives.end(); - for(auto pit = primitives.begin(); pit != pend; ++pit) + for (auto pit = primitives.begin(); pit != pend; ++pit) { float dist = pit->SqrDistance(q); - if(k_best.size() < k ) + if (k_best.size() < k) { - k_best.push(ResultEntry(dist,*pit)); + k_best.push(ResultEntry(dist, *pit)); continue; } - if(k_best.top().SqrDistance > dist) + if (k_best.top().SqrDistance > dist) { k_best.pop(); - k_best.push(ResultEntry(dist,*pit)); - } + k_best.push(ResultEntry(dist, *pit)); + } } std::vector result(k_best.size()); auto rend = result.end(); - for(auto rit = result.begin(); rit != rend; ++rit) + for (auto rit = result.begin(); rit != rend; ++rit) { *rit = k_best.top(); k_best.pop(); } return result; - } - - //closest k primitive computation - std::vector ClosestKPrimitives(size_t k,const Eigen::Vector3f& q) const + + //closest k primitive computation + std::vector ClosestKPrimitives(size_t k, const Eigen::Vector3f &q) const { //student begin - return ClosestKPrimitivesLinearSearch(k,q); + return ClosestKPrimitivesLinearSearch(k, q); //student end } - + //returns the closest primitive and its squared distance to the point q - ResultEntry ClosestPrimitive(const Eigen::Vector3f& q) const + ResultEntry ClosestPrimitive(const Eigen::Vector3f &q) const { assert(IsCompleted()); - if(root == nullptr) + if (root == nullptr) return ResultEntry(); /* Task 3.2.1 */ - return ClosestPrimitiveLinearSearch(q); + return ClosestPrimitiveLinearSearch(q); } //return the closest point position on the closest primitive in the tree with respect to the query point q - Eigen::Vector3f ClosestPoint(const Eigen::Vector3f& p) const + Eigen::Vector3f ClosestPoint(const Eigen::Vector3f &p) const { ResultEntry r = ClosestPrimitive(p); - return r.prim->ClosestPoint(p); + return r.prim->ClosestPoint(p); } - + //return the squared distance between point p and the nearest primitive in the tree - float SqrDistance(const Eigen::Vector3f& p) const + float SqrDistance(const Eigen::Vector3f &p) const { ResultEntry r = ClosestPrimitive(p); return r.SqrDistance; } //return the euclidean distance between point p and the nearest primitive in the tree - float Distance(const Eigen::Vector3f& p) const + float Distance(const Eigen::Vector3f &p) const { return sqrt(SqrDistance(p)); } - -protected: - + protected: //helper function to copy a subtree - AABBNode* CopyTree(const primitive_list& other_primitives,AABBNode* node) + AABBNode *CopyTree(const primitive_list &other_primitives, AABBNode *node) { - if(node == nullptr) + if (node == nullptr) return nullptr; - if(node->IsLeaf()) + if (node->IsLeaf()) { - AABBLeafNode* leaf = (AABBLeafNode*)node; - return new AABBLeafNode(primitives.begin()+(leaf->primitives.begin()-other_primitives.begin()), - primitives.begin()+(leaf->primitives.end()-other_primitives.begin())); + AABBLeafNode *leaf = (AABBLeafNode *)node; + return new AABBLeafNode(primitives.begin() + (leaf->primitives.begin() - other_primitives.begin()), + primitives.begin() + (leaf->primitives.end() - other_primitives.begin())); } else { - AABBSplitNode* split = (AABBSplitNode*)node; - return new AABBSplitNode(CopyTree(other_primitives,split->Left()), - CopyTree(other_primitives,split->Right())); + AABBSplitNode *split = (AABBSplitNode *)node; + return new AABBSplitNode(CopyTree(other_primitives, split->Left()), + CopyTree(other_primitives, split->Right())); } } //helper function to compute an axis aligned bounding box over the range of primitives [begin,end) Box ComputeBounds(const_primitive_iterator begin, - const_primitive_iterator end) + const_primitive_iterator end) { Box bounds; - for(auto pit = begin; pit != end; ++pit) + for (auto pit = begin; pit != end; ++pit) bounds.Insert(pit->ComputeBounds()); return bounds; } - - //recursive tree construction initially called from method complete() - //build an aabb (sub)-tree over the range of primitives [begin,end), + //build an aabb (sub)-tree over the range of primitives [begin,end), //the current bounding box is given by bounds and the current tree depth is given by the parameter depth //if depth >= max_depth or the number of primitives (end-begin) <= min_size a leaf node is constructed - //otherwise split node is created - // to create a split node the range of primitives [begin,end) must be splitted and reordered into two + //otherwise split node is created + // to create a split node the range of primitives [begin,end) must be splitted and reordered into two //sub ranges [begin,mid) and [mid,end), - //therefore sort the range of primitives [begin,end) along the largest bounding box extent by its reference + //therefore sort the range of primitives [begin,end) along the largest bounding box extent by its reference //point returned by the method ReferencePoint() - //then choose the median element as mid + //then choose the median element as mid // the STL routine std::nth_element would be very useful here , you only have to provide a ordering predicate //compute the boundg boxed of the two resulting sub ranges and recursivly call build on the two subranges //the resulting subtree are used as children of the resulting split node. - AABBNode* Build(PrimitiveIterator begin, PrimitiveIterator end, Box& bounds, int depth) + AABBNode *Build(PrimitiveIterator begin, PrimitiveIterator end, Box &bounds, int depth) { - - if(depth >= maxDepth || end-begin <= minSize) - { - return new AABBLeafNode(begin,end,bounds); + + if (depth >= maxDepth || end - begin <= minSize) + { + return new AABBLeafNode(begin, end, bounds); } Eigen::Vector3f e = bounds.Extents(); - + int axis = 0; float max_extent = e[0]; - if(max_extent < e[1]) + if (max_extent < e[1]) { axis = 1; max_extent = e[1]; } - if(max_extent < e[2]) + if (max_extent < e[2]) { axis = 2; max_extent = e[2]; } - - PrimitiveIterator mid= begin + (end-begin)/2; - std::nth_element(begin,mid,end,[&axis](const Primitive& a, const Primitive& b) - { return a.ReferencePoint()[axis] < b.ReferencePoint()[axis];}); - - Box lbounds = ComputeBounds(begin,mid); - Box rbounds = ComputeBounds(mid,end); + PrimitiveIterator mid = begin + (end - begin) / 2; + std::nth_element(begin, mid, end, [&axis](const Primitive &a, const Primitive &b) { return a.ReferencePoint()[axis] < b.ReferencePoint()[axis]; }); - return new AABBSplitNode(Build(begin,mid,lbounds,depth+1),Build(mid,end,rbounds,depth+1),bounds); + Box lbounds = ComputeBounds(begin, mid); + Box rbounds = ComputeBounds(mid, end); + return new AABBSplitNode(Build(begin, mid, lbounds, depth + 1), Build(mid, end, rbounds, depth + 1), bounds); } }; //helper function to construct an aabb tree data structure from the triangle faces of the halfedge mesh m -void BuildAABBTreeFromTriangles(const HEMesh& m, AABBTree& tree); +void BuildAABBTreeFromTriangles(const HEMesh &m, AABBTree &tree); //helper function to construct an aabb tree data structure from the vertices of the halfedge mesh m -void BuildAABBTreeFromVertices(const HEMesh& m, AABBTree& tree); +void BuildAABBTreeFromVertices(const HEMesh &m, AABBTree &tree); //helper function to construct an aabb tree data structure from the edges of the halfedge mesh m -void BuildAABBTreeFromEdges(const HEMesh& m, AABBTree& tree); - +void BuildAABBTreeFromEdges(const HEMesh &m, AABBTree &tree); diff --git a/exercise4/include/Box.h b/exercise4/include/Box.h index 114472e..a97c5c6 100644 --- a/exercise4/include/Box.h +++ b/exercise4/include/Box.h @@ -1,5 +1,5 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved #pragma once @@ -11,26 +11,25 @@ class Box //internal storage for lower and upper corner points of the box std::pair bounds; -public: - + public: //creates an empty box like the method Clear Box(); //construct a box with the gven lower and upper corner points - Box(const Eigen::Vector3f& lbound, const Eigen::Vector3f& ubound); + Box(const Eigen::Vector3f &lbound, const Eigen::Vector3f &ubound); //returns the corner point which is the lower bound of the box in all dimensions - Eigen::Vector3f& LowerBound(); + Eigen::Vector3f &LowerBound(); //returns the corner point which is the lower bound of the box in all dimensions - const Eigen::Vector3f& LowerBound() const; + const Eigen::Vector3f &LowerBound() const; //returns the corner point which is the upper bound of the box in all dimensions - Eigen::Vector3f& UpperBound(); + Eigen::Vector3f &UpperBound(); //returns the corner point which is the upper bound of the box in all dimensions - const Eigen::Vector3f& UpperBound() const; - + const Eigen::Vector3f &UpperBound() const; + //returns a vector containing the extents of the box in all dimensions Eigen::Vector3f Extents() const; @@ -46,36 +45,35 @@ public: //returns the volume of the box float Volume() const; - //returns the box radius for a given direction vector a - //if the box is centered at the origin + //returns the box radius for a given direction vector a + //if the box is centered at the origin //then the projection of the box on direction a is contained within the Interval [-r,r] - float Radius(const Eigen::Vector3f& a) const; + float Radius(const Eigen::Vector3f &a) const; //returns true if the box b overlaps with the current one - bool Overlaps(const Box& b) const; + bool Overlaps(const Box &b) const; //returns true if the point p is inside this box - bool IsInside(const Eigen::Vector3f& p) const; + bool IsInside(const Eigen::Vector3f &p) const; //returns true if box b is inside this box - bool IsInside(const Box& b) const; - + bool IsInside(const Box &b) const; + //creates a box which goes from [+infinity,- infinity] in al dimensions void Clear(); - + //enlarges the box such that the point p is inside afterwards - void Insert(const Eigen::Vector3f& p); + void Insert(const Eigen::Vector3f &p); //enlarges the box such that box b is inside afterwards - void Insert(const Box& b); + void Insert(const Box &b); - //returns the point on or inside the box with the smallest distance to p - Eigen::Vector3f ClosestPoint(const Eigen::Vector3f& p) const; + //returns the point on or inside the box with the smallest distance to p + Eigen::Vector3f ClosestPoint(const Eigen::Vector3f &p) const; - //returns the squared distance between p and the box - float SqrDistance(const Eigen::Vector3f& p) const; - - //returns the euclidean distance between p and the box - float Distance(const Eigen::Vector3f& p) const; + //returns the squared distance between p and the box + float SqrDistance(const Eigen::Vector3f &p) const; + //returns the euclidean distance between p and the box + float Distance(const Eigen::Vector3f &p) const; }; diff --git a/exercise4/include/GridTraverser.h b/exercise4/include/GridTraverser.h index 0f41bc9..c369dfb 100644 --- a/exercise4/include/GridTraverser.h +++ b/exercise4/include/GridTraverser.h @@ -1,5 +1,5 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved #pragma once @@ -10,7 +10,7 @@ class GridTraverser { //ray origin and direction - Eigen::Vector3f orig,dir; + Eigen::Vector3f orig, dir; //grid cell extents Eigen::Vector3f cellExtents; //current cell index @@ -18,27 +18,27 @@ class GridTraverser /* you can additional attributes for incremental calculation here */ -public: + public: //default constructor GridTraverser(); //constructs a grid traverser for a given ray with origin o, and ray direction d for a grid with cell extents ce - GridTraverser(const Eigen::Vector3f& o, const Eigen::Vector3f&d, const Eigen::Vector3f ce); + GridTraverser(const Eigen::Vector3f &o, const Eigen::Vector3f &d, const Eigen::Vector3f ce); //accessor of ray origin - Eigen::Vector3f& Origin(); + Eigen::Vector3f &Origin(); //const accessor of ray origin - const Eigen::Vector3f& Origin() const; + const Eigen::Vector3f &Origin() const; //accessor of ray direction - Eigen::Vector3f& Direction(); - + Eigen::Vector3f &Direction(); + //const accessor of ray direction - const Eigen::Vector3f& Direction() const; + const Eigen::Vector3f &Direction() const; //set cell extents - void SetCellExtents(const Eigen::Vector3f& cellExtent); + void SetCellExtents(const Eigen::Vector3f &cellExtent); //init at origin cell void Init(); @@ -47,5 +47,5 @@ public: void operator++(int); //return current cell index - Eigen::Vector3i operator*(); + Eigen::Vector3i operator*(); }; diff --git a/exercise4/include/GridUtils.h b/exercise4/include/GridUtils.h index 5b5b5ac..8f454a6 100644 --- a/exercise4/include/GridUtils.h +++ b/exercise4/include/GridUtils.h @@ -1,5 +1,5 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved #pragma once @@ -8,7 +8,7 @@ #include //converts 3d floating point position pos into 3d integer grid cell index -inline Eigen::Vector3i PositionToCellIndex(const Eigen::Vector3f& pos, const Eigen::Vector3f& cellExtents) +inline Eigen::Vector3i PositionToCellIndex(const Eigen::Vector3f& pos, const Eigen::Vector3f& cellExtents) { Eigen::Vector3i idx; for(int d = 0; d < 3; ++d) @@ -17,18 +17,18 @@ inline Eigen::Vector3i PositionToCellIndex(const Eigen::Vector3f& pos, const Eig if (pos[d] < 0) --idx[d]; } - return idx; + return idx; } -//returns true if the two Interval [lb1,ub2] and [lb2,ub2] overlap +//returns true if the two Interval [lb1,ub2] and [lb2,ub2] overlap inline bool OverlapIntervals(float lb1, float ub1, float lb2, float ub2) { - if(lb1 > ub1) + if(lb1 > ub1) std::swap(lb1,ub1); - if(lb2 > ub2) + if(lb2 > ub2) std::swap(lb2,ub2); if(ub1 < lb2|| lb1 >ub2) return false; - return true; + return true; } diff --git a/exercise4/include/HashGrid.h b/exercise4/include/HashGrid.h index 431485c..4c089ff 100644 --- a/exercise4/include/HashGrid.h +++ b/exercise4/include/HashGrid.h @@ -1,5 +1,5 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved #pragma once @@ -14,9 +14,9 @@ #include "LineSegment.h" template -class HashGrid +class HashGrid { -public: +public: //hash function struct GridHashFunc @@ -30,8 +30,8 @@ public: } }; //type of internal hash map - typedef std::unordered_map,GridHashFunc> CellHashMapType; - + typedef std::unordered_map,GridHashFunc> CellHashMapType; + private: //internal hash map storing the data of each non empty grid cell //it is a map with a 3 dimensional cell index index as a key and a std::vector as value type @@ -43,14 +43,14 @@ public: //constructor for hash grid with uniform cell extent //initial size is used to preallocate memory for the internal unordered map HashGrid(const float cellExtent=0.01,const int initialSize=1): cellHashMap(initialSize) - { + { cellExtents[0] =cellExtents[1] =cellExtents[2] = cellExtent; } //constructor for hash grid with non uniform cell extents //initial size is used to preallocate memory for the internal unordered map HashGrid(const Eigen::Vector3f& cellExtents,const int initialSize): cellHashMap(initialSize),cellExtents(cellExtents) - { + { } //resize hash map with at least count buckets @@ -59,10 +59,10 @@ public: cellHashMap.rehash(count); } - - //converts a position to a grid index + + //converts a position to a grid index Eigen::Vector3i PositionToIndex(const Eigen::Vector3f& pos) const - { + { return PositionToCellIndex(pos, cellExtents) ; } @@ -72,7 +72,7 @@ public: Eigen::Vector3f p; for(int d = 0; d < 3; ++d) p[d] = (idx[d] + 0.5f)*cellExtents[d]; - + return p; } @@ -81,14 +81,14 @@ public: { return CellCenter(PositionToIndex(pos)); } - + //return the min corner position of a cell specified by its cell key Eigen::Vector3f CellMinPosition(const Eigen::Vector3i& key) const { Eigen::Vector3f p; for(int d = 0; d < 3; ++d) p[d] = key[d]*cellExtents[d]; - + return p; } @@ -99,12 +99,12 @@ public: } //return the max corner position of a cell specified by its cell key - Eigen::Vector3f CellMaxPosition(const Eigen::Vector3i& idx) const + Eigen::Vector3f CellMaxPosition(const Eigen::Vector3i& idx) const { Eigen::Vector3f p; for(int d = 0; d < 3; ++d) p[d] = (idx[d]+1)*cellExtents[d]; - + return p; } @@ -117,16 +117,16 @@ public: //returns bounding box of cell with index idx Box CellBounds(const Eigen::Vector3i& idx) const { - return Box(CellMinPosition(idx),CellMaxPosition(idx)); + return Box(CellMinPosition(idx),CellMaxPosition(idx)); } - + //returns the bounding box of cell containing the point pos Box CellBounds(const Eigen::Vector3f& pos) const { Eigen::Vector3i idx = PositionToIndex(pos); - return Box(CellMinPosition(idx),CellMaxPosition(idx)); + return Box(CellMinPosition(idx),CellMaxPosition(idx)); } - + //returns the extents of a grid cell Eigen::Vector3f CellExtents() const { @@ -141,14 +141,14 @@ public: vol *= cellExtents[d]; return vol; } - + //removes all non empty cells from the hash grid bool Empty(const Eigen::Vector3i& idx) const { auto it = cellHashMap.find(idx); if(it == cellHashMap.end()) return true; - return false; + return false; } @@ -169,8 +169,8 @@ public: Eigen::Vector3i lb_idx = PositionToIndex(lb); Eigen::Vector3i ub_idx = PositionToIndex(ub); - - + + Eigen::Vector3i idx; for(idx[0] = lb_idx[0]; idx[0] <=ub_idx[0]; ++idx[0]) for(idx[1] = lb_idx[1]; idx[1] <=ub_idx[1]; ++idx[1]) @@ -179,14 +179,14 @@ public: cellHashMap[idx].push_back(p); } - + //remove all cells from hash grid void Clear() { cellHashMap.clear(); } - + //returns true if hashgrid contains no cells bool Empty() const { diff --git a/exercise4/include/LineSegment.h b/exercise4/include/LineSegment.h index 9130023..cbe65af 100644 --- a/exercise4/include/LineSegment.h +++ b/exercise4/include/LineSegment.h @@ -1,5 +1,5 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved #pragma once @@ -19,34 +19,32 @@ class LineSegment //internal storage for an edge handle //this edge handle can be used to optionally identify the edge in a halfedge mesh data structure instance OpenMesh::EdgeHandle h; - -public: + + public: //default constructor LineSegment(); //constructs a line segment by the two end points v0, v1 without using the edge handle - LineSegment(const Eigen::Vector3f& v0, const Eigen::Vector3f& v1); + LineSegment(const Eigen::Vector3f &v0, const Eigen::Vector3f &v1); //construct a line segment from the edge e of the halfedge mesh m - LineSegment(const HEMesh& m,const OpenMesh::EdgeHandle& e); + LineSegment(const HEMesh &m, const OpenMesh::EdgeHandle &e); //returns an axis aligned bounding box of the line segment Box ComputeBounds() const; - + //returns true if the line segment overlaps the given box b - bool Overlaps(const Box& b) const; + bool Overlaps(const Box &b) const; //returns the point with smallest distance topoint p which lies on the line segment - Eigen::Vector3f ClosestPoint(const Eigen::Vector3f& p) const; + Eigen::Vector3f ClosestPoint(const Eigen::Vector3f &p) const; //returns the squared distance between point p and the line segment - float SqrDistance(const Eigen::Vector3f& p) const; + float SqrDistance(const Eigen::Vector3f &p) const; //returns the euclidean distance between point p and the line segment - float Distance(const Eigen::Vector3f& p) const; - + float Distance(const Eigen::Vector3f &p) const; + //returns a reference point which is on the line segment and is used to sort the primitive in the AABB tree construction Eigen::Vector3f ReferencePoint() const; - }; - diff --git a/exercise4/include/Point.h b/exercise4/include/Point.h index 174f416..1e3150b 100644 --- a/exercise4/include/Point.h +++ b/exercise4/include/Point.h @@ -1,5 +1,5 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved #pragma once @@ -14,19 +14,19 @@ class Point //internal storage for a vertex handle //this vertex handle can be used to optionally identify the vertex in a halfedge mesh data structure instance OpenMesh::VertexHandle h; - + public: - + //default constructor Point(); - + //construct a point with given point position v0 Point(const Eigen::Vector3f& v0); //construct a point from vertex v of giben halfedge mesh m Point(const HEMesh &m, const OpenMesh::VertexHandle& v); - + //returns axis aligned bounding box of point Box ComputeBounds() const; diff --git a/exercise4/include/Triangle.h b/exercise4/include/Triangle.h index 078f7c6..70e361b 100644 --- a/exercise4/include/Triangle.h +++ b/exercise4/include/Triangle.h @@ -1,12 +1,11 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved #pragma once #include "Box.h" #include "util/OpenMeshUtils.h" - /* a triangle primitive which can be used with the AABBTree and the HashGrid data structure */ @@ -21,28 +20,25 @@ class Triangle //internal storage of the optional face handle to identify the originating face in a half edge mesh instance OpenMesh::FaceHandle h; -public: - + public: //default constructor Triangle(); //constructs a triangle using the vertex positions v0,v1 and v2 - Triangle(const Eigen::Vector3f& v0, const Eigen::Vector3f& v1,const Eigen::Vector3f& v2); + Triangle(const Eigen::Vector3f &v0, const Eigen::Vector3f &v1, const Eigen::Vector3f &v2); //constructs a triangle from the face f of the given halfedge mesh m - Triangle(const HEMesh&m, const OpenMesh::FaceHandle& f); + Triangle(const HEMesh &m, const OpenMesh::FaceHandle &f); //returns the axis aligned bounding box of the triangle Box ComputeBounds() const; //returns true if the triangle overlaps the given box b - bool Overlaps(const Box& b) const; + bool Overlaps(const Box &b) const; //returns the barycentric coordinates of the point with thesmallest distance to point p which lies on the triangle - void ClosestPointBarycentric(const Eigen::Vector3f& p, float& l0, float& l1, float& l2) const; + void ClosestPointBarycentric(const Eigen::Vector3f &p, float &l0, float &l1, float &l2) const; //returns the point with smallest distance to point p which lies on the triangle - Eigen::Vector3f ClosestPoint(const Eigen::Vector3f& p) const; + Eigen::Vector3f ClosestPoint(const Eigen::Vector3f &p) const; //returns the squared distance between point p and the triangle - float SqrDistance(const Eigen::Vector3f& p) const; + float SqrDistance(const Eigen::Vector3f &p) const; //returns the euclidean distance between point p and the triangle - float Distance(const Eigen::Vector3f& p) const; + float Distance(const Eigen::Vector3f &p) const; //returns a reference point which is on the triangle and is used to sort the primitive in the AABB tree construction Eigen::Vector3f ReferencePoint() const; - }; - diff --git a/exercise4/include/Viewer.h b/exercise4/include/Viewer.h index fcda12f..f531631 100644 --- a/exercise4/include/Viewer.h +++ b/exercise4/include/Viewer.h @@ -1,5 +1,5 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved #pragma once @@ -18,27 +18,28 @@ class Viewer : public nse::gui::AbstractViewer { -public: + public: Viewer(); void drawContents(); -private: - + private: enum PrimitiveType { - Vertex, Edge, Tri + Vertex, + Edge, + Tri }; void SetupGUI(); void MeshUpdated(); - void FindClosestPoint(const Eigen::Vector3f& p); + void FindClosestPoint(const Eigen::Vector3f &p); void BuildGridVBO(); void BuildRayVBOs(); template - void BuildGridVBO(const Grid& grid) + void BuildGridVBO(const Grid &grid) { std::vector positions; for (auto it = grid.NonEmptyCellsBegin(); it != grid.NonEmptyCellsEnd(); ++it) @@ -54,25 +55,25 @@ private: gridIndices = (GLuint)positions.size(); } - void AddBoxVertices(const Box& box, std::vector& positions); + void AddBoxVertices(const Box &box, std::vector &positions); - nanogui::ComboBox* shadingBtn; - nanogui::CheckBox* chkRenderMesh; - nanogui::CheckBox* chkRenderGrid; - nanogui::CheckBox* chkRenderRay; + nanogui::ComboBox *shadingBtn; + nanogui::CheckBox *chkRenderMesh; + nanogui::CheckBox *chkRenderGrid; + nanogui::CheckBox *chkRenderRay; int raySteps; - nse::gui::VectorInput* sldQuery, *sldRayOrigin, *sldRayDir; - nanogui::ComboBox* cmbPrimitiveType; - + nse::gui::VectorInput *sldQuery, *sldRayOrigin, *sldRayDir; + nanogui::ComboBox *cmbPrimitiveType; + HEMesh polymesh; float bboxMaxLength; MeshRenderer renderer; - + AABBTree vertexTree; AABBTree edgeTree; AABBTree triangleTree; - + HashGrid vertexGrid; HashGrid edgeGrid; HashGrid triangleGrid; diff --git a/exercise4/src/AABBTree.cpp b/exercise4/src/AABBTree.cpp index dd09c4f..37896a0 100644 --- a/exercise4/src/AABBTree.cpp +++ b/exercise4/src/AABBTree.cpp @@ -1,42 +1,42 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved #include "AABBTree.h" #include -void BuildAABBTreeFromTriangles(const HEMesh& m, AABBTree& tree) +void BuildAABBTreeFromTriangles(const HEMesh &m, AABBTree &tree) { std::cout << "Building AABB tree from triangles .." << std::endl; tree.Clear(); auto fend = m.faces_end(); - for(auto fit = m.faces_begin(); fit != fend; ++fit) - tree.Insert(Triangle(m,*fit)); - + for (auto fit = m.faces_begin(); fit != fend; ++fit) + tree.Insert(Triangle(m, *fit)); + tree.Complete(); std::cout << "Done." << std::endl; } -void BuildAABBTreeFromVertices(const HEMesh& m, AABBTree& tree) +void BuildAABBTreeFromVertices(const HEMesh &m, AABBTree &tree) { std::cout << "Building AABB tree from vertices .." << std::endl; tree.Clear(); auto vend = m.vertices_end(); - for(auto vit = m.vertices_begin(); vit != vend; ++vit) - tree.Insert(Point(m,*vit)); - + for (auto vit = m.vertices_begin(); vit != vend; ++vit) + tree.Insert(Point(m, *vit)); + tree.Complete(); std::cout << "Done." << std::endl; } -void BuildAABBTreeFromEdges(const HEMesh& m, AABBTree& tree) +void BuildAABBTreeFromEdges(const HEMesh &m, AABBTree &tree) { std::cout << "Building AABB tree from edges .." << std::endl; tree.Clear(); auto eend = m.edges_end(); - for(auto eit = m.edges_begin(); eit != eend; ++eit) - tree.Insert(LineSegment(m,*eit)); - + for (auto eit = m.edges_begin(); eit != eend; ++eit) + tree.Insert(LineSegment(m, *eit)); + tree.Complete(); std::cout << "Done." << std::endl; } diff --git a/exercise4/src/Box.cpp b/exercise4/src/Box.cpp index 0248a8e..cdc7c56 100644 --- a/exercise4/src/Box.cpp +++ b/exercise4/src/Box.cpp @@ -1,12 +1,11 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved #include "Box.h" #include "GridUtils.h" #include - //creates an empty box like the method Clear Box::Box() { @@ -14,31 +13,31 @@ Box::Box() } //construct a box with the gven lower and upper corner points -Box::Box(const Eigen::Vector3f& lbound, const Eigen::Vector3f& ubound) +Box::Box(const Eigen::Vector3f &lbound, const Eigen::Vector3f &ubound) { - bounds = std::make_pair(lbound,ubound); + bounds = std::make_pair(lbound, ubound); } //returns the corner point which is the lower bound of the box in all dimensions -Eigen::Vector3f& Box::LowerBound() +Eigen::Vector3f &Box::LowerBound() { return bounds.first; } //returns the corner point which is the lower bound of the box in all dimensions -const Eigen::Vector3f& Box::LowerBound() const +const Eigen::Vector3f &Box::LowerBound() const { return bounds.first; } //returns the corner point which is the upper bound of the box in all dimensions -Eigen::Vector3f& Box::UpperBound() +Eigen::Vector3f &Box::UpperBound() { return bounds.second; } //returns the corner point which is the upper bound of the box in all dimensions -const Eigen::Vector3f& Box::UpperBound() const +const Eigen::Vector3f &Box::UpperBound() const { return bounds.second; } @@ -46,76 +45,76 @@ const Eigen::Vector3f& Box::UpperBound() const //returns a vector containing the extents of the box in all dimensions Eigen::Vector3f Box::Extents() const { - return UpperBound()-LowerBound(); + return UpperBound() - LowerBound(); } //returns a vector containing the extents of the box in all dimensions divided by 2 Eigen::Vector3f Box::HalfExtents() const { - return Extents()/2.0f; + return Extents() / 2.0f; } //returns the center of the box Eigen::Vector3f Box::Center() const { - return (LowerBound()+UpperBound())/2.0f; + return (LowerBound() + UpperBound()) / 2.0f; } //returns the surface area of the box float Box::SurfaceArea() const { Eigen::Vector3f e = Extents(); - return 2.0f*(e[0]*e[1] + e[1]*e[2] + e[0]*e[2]); + return 2.0f * (e[0] * e[1] + e[1] * e[2] + e[0] * e[2]); } //returns the volume of the box float Box::Volume() const { Eigen::Vector3f e = Extents(); - return e[0] + e[1]+ e[2]; + return e[0] + e[1] + e[2]; } -//returns the box radius for a given direction vector a -//if the box is centered at the origin +//returns the box radius for a given direction vector a +//if the box is centered at the origin //then the projection of the box on direction a is contained within the Interval [-r,r] -float Box::Radius(const Eigen::Vector3f& a) const +float Box::Radius(const Eigen::Vector3f &a) const { Eigen::Vector3f h = HalfExtents(); - return h[0]*std::abs(a[0]) + h[1]*std::abs(a[1]) + h[2]*std::abs(a[2]); + return h[0] * std::abs(a[0]) + h[1] * std::abs(a[1]) + h[2] * std::abs(a[2]); } //returns true if the box b overlaps with the current one -bool Box:: Overlaps(const Box& b) const +bool Box::Overlaps(const Box &b) const { Eigen::Vector3f lb1 = LowerBound(); Eigen::Vector3f ub1 = UpperBound(); Eigen::Vector3f lb2 = b.LowerBound(); Eigen::Vector3f ub2 = b.UpperBound(); - - if(!OverlapIntervals(lb1[0], ub1[0],lb2[0], ub2[0])) + + if (!OverlapIntervals(lb1[0], ub1[0], lb2[0], ub2[0])) return false; - if(!OverlapIntervals(lb1[1], ub1[1],lb2[1], ub2[1])) + if (!OverlapIntervals(lb1[1], ub1[1], lb2[1], ub2[1])) return false; - if(!OverlapIntervals(lb1[2], ub1[2],lb2[2], ub2[2])) + if (!OverlapIntervals(lb1[2], ub1[2], lb2[2], ub2[2])) return false; - + return true; } //returns true if the point p is inside this box -bool Box::IsInside(const Eigen::Vector3f& p) const +bool Box::IsInside(const Eigen::Vector3f &p) const { - const Eigen::Vector3f& lbound = LowerBound(); - const Eigen::Vector3f& ubound = UpperBound(); - if(p[0] < lbound[0] || p[0] > ubound[0] || + const Eigen::Vector3f &lbound = LowerBound(); + const Eigen::Vector3f &ubound = UpperBound(); + if (p[0] < lbound[0] || p[0] > ubound[0] || p[1] < lbound[1] || p[1] > ubound[1] || p[2] < lbound[2] || p[2] > ubound[2]) return false; - return true; + return true; } //returns true if box b is inside this box -bool Box::IsInside(const Box& b) const +bool Box::IsInside(const Box &b) const { return IsInside(b.LowerBound()) && IsInside(b.UpperBound()); } @@ -124,55 +123,53 @@ bool Box::IsInside(const Box& b) const void Box::Clear() { const float infty = std::numeric_limits::infinity(); - const Eigen::Vector3f vec_infty = Eigen::Vector3f(infty,infty,infty); - bounds = std::make_pair(vec_infty,-vec_infty); + const Eigen::Vector3f vec_infty = Eigen::Vector3f(infty, infty, infty); + bounds = std::make_pair(vec_infty, -vec_infty); } - + //enlarges the box such that the point p is inside afterwards -void Box::Insert(const Eigen::Vector3f& p) +void Box::Insert(const Eigen::Vector3f &p) { - Eigen::Vector3f& lbound = LowerBound(); - Eigen::Vector3f& ubound = UpperBound(); - lbound[0] = (std::min)(p[0],lbound[0]); - lbound[1] = (std::min)(p[1],lbound[1]); - lbound[2] = (std::min)(p[2],lbound[2]); - ubound[0] = (std::max)(p[0],ubound[0]); - ubound[1] = (std::max)(p[1],ubound[1]); - ubound[2] = (std::max)(p[2],ubound[2]); + Eigen::Vector3f &lbound = LowerBound(); + Eigen::Vector3f &ubound = UpperBound(); + lbound[0] = (std::min)(p[0], lbound[0]); + lbound[1] = (std::min)(p[1], lbound[1]); + lbound[2] = (std::min)(p[2], lbound[2]); + ubound[0] = (std::max)(p[0], ubound[0]); + ubound[1] = (std::max)(p[1], ubound[1]); + ubound[2] = (std::max)(p[2], ubound[2]); } //enlarges the box such that box b is inside afterwards -void Box::Insert(const Box& b) +void Box::Insert(const Box &b) { Insert(b.LowerBound()); Insert(b.UpperBound()); } -//returns the point on or inside the box with the smallest distance to p -Eigen::Vector3f Box::ClosestPoint(const Eigen::Vector3f& p) const +//returns the point on or inside the box with the smallest distance to p +Eigen::Vector3f Box::ClosestPoint(const Eigen::Vector3f &p) const { Eigen::Vector3f q; - const Eigen::Vector3f& lbound = LowerBound(); - const Eigen::Vector3f& ubound = UpperBound(); - - q[0] = std::min(std::max(p[0],lbound[0]),ubound[0]); - q[1] = std::min(std::max(p[1],lbound[1]),ubound[1]); - q[2] = std::min(std::max(p[2],lbound[2]),ubound[2]); - + const Eigen::Vector3f &lbound = LowerBound(); + const Eigen::Vector3f &ubound = UpperBound(); + + q[0] = std::min(std::max(p[0], lbound[0]), ubound[0]); + q[1] = std::min(std::max(p[1], lbound[1]), ubound[1]); + q[2] = std::min(std::max(p[2], lbound[2]), ubound[2]); + return q; } -//returns the squared distance between p and the box -float Box::SqrDistance(const Eigen::Vector3f& p) const +//returns the squared distance between p and the box +float Box::SqrDistance(const Eigen::Vector3f &p) const { - Eigen::Vector3f d = p-ClosestPoint(p); + Eigen::Vector3f d = p - ClosestPoint(p); return d.dot(d); } -//returns the euclidean distance between p and the box -float Box::Distance(const Eigen::Vector3f& p) const +//returns the euclidean distance between p and the box +float Box::Distance(const Eigen::Vector3f &p) const { return sqrt(SqrDistance(p)); } - - diff --git a/exercise4/src/GridTraverser.cpp b/exercise4/src/GridTraverser.cpp index 8173193..e302cf2 100644 --- a/exercise4/src/GridTraverser.cpp +++ b/exercise4/src/GridTraverser.cpp @@ -1,41 +1,41 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved #include "GridTraverser.h" #include "GridUtils.h" - GridTraverser::GridTraverser() -{ } +{ +} -GridTraverser::GridTraverser(const Eigen::Vector3f& o, const Eigen::Vector3f&d, const Eigen::Vector3f cell_extents) +GridTraverser::GridTraverser(const Eigen::Vector3f &o, const Eigen::Vector3f &d, const Eigen::Vector3f cell_extents) : orig(o), dir(d), cellExtents(cell_extents) { dir.normalize(); Init(); } -Eigen::Vector3f& GridTraverser::Origin() +Eigen::Vector3f &GridTraverser::Origin() { return orig; } -const Eigen::Vector3f& GridTraverser::Origin() const +const Eigen::Vector3f &GridTraverser::Origin() const { return orig; } -Eigen::Vector3f& GridTraverser::Direction() +Eigen::Vector3f &GridTraverser::Direction() { return dir; } -const Eigen::Vector3f& GridTraverser::Direction() const +const Eigen::Vector3f &GridTraverser::Direction() const { return dir; } -void GridTraverser::SetCellExtents(const Eigen::Vector3f& cellExtent) +void GridTraverser::SetCellExtents(const Eigen::Vector3f &cellExtent) { this->cellExtents = cellExtent; Init(); @@ -59,5 +59,3 @@ Eigen::Vector3i GridTraverser::operator*() { return current; } - - diff --git a/exercise4/src/HashGrid.cpp b/exercise4/src/HashGrid.cpp index e47649a..60ddd60 100644 --- a/exercise4/src/HashGrid.cpp +++ b/exercise4/src/HashGrid.cpp @@ -1,36 +1,36 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved #include "HashGrid.h" #include -void BuildHashGridFromTriangles(const HEMesh& m, HashGrid& grid, const Eigen::Vector3f& cellSize) +void BuildHashGridFromTriangles(const HEMesh &m, HashGrid &grid, const Eigen::Vector3f &cellSize) { std::cout << "Building hash grid from triangles .." << std::endl; grid = HashGrid(cellSize, 1); auto fend = m.faces_end(); - for(auto fit = m.faces_begin(); fit != fend; ++fit) - grid.Insert(Triangle(m,*fit)); + for (auto fit = m.faces_begin(); fit != fend; ++fit) + grid.Insert(Triangle(m, *fit)); std::cout << "Done (using " << grid.NumCells() << " cells)." << std::endl; } -void BuildHashGridFromVertices(const HEMesh& m, HashGrid& grid, const Eigen::Vector3f& cellSize) +void BuildHashGridFromVertices(const HEMesh &m, HashGrid &grid, const Eigen::Vector3f &cellSize) { std::cout << "Building hash grid from vertices .." << std::endl; grid = HashGrid(cellSize, 1); auto vend = m.vertices_end(); - for(auto vit = m.vertices_begin(); vit != vend; ++vit) - grid.Insert(Point(m,*vit)); + for (auto vit = m.vertices_begin(); vit != vend; ++vit) + grid.Insert(Point(m, *vit)); std::cout << "Done (using " << grid.NumCells() << " cells)." << std::endl; } -void BuildHashGridFromEdges(const HEMesh& m, HashGrid& grid, const Eigen::Vector3f& cellSize) +void BuildHashGridFromEdges(const HEMesh &m, HashGrid &grid, const Eigen::Vector3f &cellSize) { std::cout << "Building hash grid from edges .." << std::endl; grid = HashGrid(cellSize, 1); auto eend = m.edges_end(); - for(auto eit = m.edges_begin(); eit != eend; ++eit) - grid.Insert(LineSegment(m,*eit)); + for (auto eit = m.edges_begin(); eit != eend; ++eit) + grid.Insert(LineSegment(m, *eit)); std::cout << "Done (using " << grid.NumCells() << " cells)." << std::endl; } diff --git a/exercise4/src/LineSegment.cpp b/exercise4/src/LineSegment.cpp index a098ceb..85562f7 100644 --- a/exercise4/src/LineSegment.cpp +++ b/exercise4/src/LineSegment.cpp @@ -1,23 +1,22 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved #include "LineSegment.h" #include "GridUtils.h" - //default constructor LineSegment::LineSegment() { } //constructs a line segment by the two end points v0, v1 without using the edge handle -LineSegment::LineSegment(const Eigen::Vector3f& v0, const Eigen::Vector3f& v1):v0(v0),v1(v1) +LineSegment::LineSegment(const Eigen::Vector3f &v0, const Eigen::Vector3f &v1) : v0(v0), v1(v1) { } - + //construct a line segment from the edge e of the halfedge mesh m -LineSegment::LineSegment(const HEMesh& m,const OpenMesh::EdgeHandle& e):h(e) +LineSegment::LineSegment(const HEMesh &m, const OpenMesh::EdgeHandle &e) : h(e) { auto h = m.halfedge_handle(e, 0); v0 = ToEigenVector(m.point(m.from_vertex_handle(h))); @@ -27,97 +26,91 @@ LineSegment::LineSegment(const HEMesh& m,const OpenMesh::EdgeHandle& e):h(e) //returns an axis aligned bounding box of the line segment Box LineSegment::ComputeBounds() const { - Box b; + Box b; b.Insert(v0); b.Insert(v1); return b; } - + //returns true if the line segment overlaps the given box b -bool LineSegment::Overlaps(const Box& b) const +bool LineSegment::Overlaps(const Box &b) const { Box aabb = ComputeBounds(); - if(!b.Overlaps(aabb)) + if (!b.Overlaps(aabb)) return false; - + Eigen::Vector3f o = b.Center(); - Eigen::Vector3f u1 = v0-o; - Eigen::Vector3f u2 = v1-o; - - Eigen::Vector3f d1 = v1-v0; + Eigen::Vector3f u1 = v0 - o; + Eigen::Vector3f u2 = v1 - o; + + Eigen::Vector3f d1 = v1 - v0; d1.normalize(); - + float r = b.Radius(d1); float lb = u1.dot(d1); float ub = u2.dot(d1); - if(lb > ub) - std::swap(lb,ub); - if(lb > r || ub < -r) + if (lb > ub) + std::swap(lb, ub); + if (lb > r || ub < -r) return false; - Eigen::Vector3f e1(1,0,0); - Eigen::Vector3f d2= d1.cross(e1); + Eigen::Vector3f e1(1, 0, 0); + Eigen::Vector3f d2 = d1.cross(e1); r = b.Radius(d2); lb = u1.dot(d2); ub = u2.dot(d2); - if(lb > ub) - std::swap(lb,ub); - if(!OverlapIntervals(-r,r,lb,ub)) + if (lb > ub) + std::swap(lb, ub); + if (!OverlapIntervals(-r, r, lb, ub)) return false; - Eigen::Vector3f e2(0,1,0); + Eigen::Vector3f e2(0, 1, 0); Eigen::Vector3f d3 = d1.cross(e2); r = b.Radius(d3); lb = u1.dot(d3); ub = u2.dot(d3); - if(lb > ub) - std::swap(lb,ub); - if(!OverlapIntervals(-r,r,lb,ub)) + if (lb > ub) + std::swap(lb, ub); + if (!OverlapIntervals(-r, r, lb, ub)) return false; - Eigen::Vector3f e3(0,0,1); + Eigen::Vector3f e3(0, 0, 1); Eigen::Vector3f d4 = d1.cross(e3); r = b.Radius(d4); lb = u1.dot(d4); ub = u2.dot(d4); - - if(!OverlapIntervals(-r,r,lb,ub)) + + if (!OverlapIntervals(-r, r, lb, ub)) return false; - return true; - - } //returns the point with smallest distance topoint p which lies on the line segment -Eigen::Vector3f LineSegment::ClosestPoint(const Eigen::Vector3f& p) const +Eigen::Vector3f LineSegment::ClosestPoint(const Eigen::Vector3f &p) const { //the two endpoints of the line segment are v0,v1 /* Task 3.2.1 */ - return Eigen::Vector3f(0,0,0); + return Eigen::Vector3f(0, 0, 0); } //returns the squared distance between point p and the line segment -float LineSegment::SqrDistance(const Eigen::Vector3f& p) const +float LineSegment::SqrDistance(const Eigen::Vector3f &p) const { - Eigen::Vector3f d = p-ClosestPoint(p); + Eigen::Vector3f d = p - ClosestPoint(p); return d.squaredNorm(); } //returns the euclidean distance between point p and the line segment -float LineSegment::Distance(const Eigen::Vector3f& p) const +float LineSegment::Distance(const Eigen::Vector3f &p) const { return sqrt(SqrDistance(p)); } - + //returns a reference point which is on the line segment and is used to sort the primitive in the AABB tree construction Eigen::Vector3f LineSegment::ReferencePoint() const { - return 0.5f*(v0 + v1); + return 0.5f * (v0 + v1); } - - - diff --git a/exercise4/src/Point.cpp b/exercise4/src/Point.cpp index fdbc37d..74ff4a0 100644 --- a/exercise4/src/Point.cpp +++ b/exercise4/src/Point.cpp @@ -1,59 +1,58 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved #include "Point.h" #include "GridUtils.h" //default constructor -Point::Point(){} - +Point::Point() {} + //construct a point with given point position v0 -Point::Point(const Eigen::Vector3f& v0):v0(v0) +Point::Point(const Eigen::Vector3f &v0) : v0(v0) { } //construct a point from vertex v of giben halfedge mesh m -Point::Point(const HEMesh &m, const OpenMesh::VertexHandle& v):h(v) +Point::Point(const HEMesh &m, const OpenMesh::VertexHandle &v) : h(v) { v0 = ToEigenVector(m.point(v)); } - + //returns axis aligned bounding box of point Box Point::ComputeBounds() const { - Box b; + Box b; b.Insert(v0); - + return b; } //returns true if point overlap with box b -bool Point::Overlaps(const Box& b) const +bool Point::Overlaps(const Box &b) const { Eigen::Vector3f lb = b.LowerBound(); Eigen::Vector3f ub = b.UpperBound(); - return - (v0[0] >= lb[0] && v0[0] <= ub[0]&& - v0[1] >= lb[1] && v0[1] <= ub[1] && - v0[2] >= lb[2] && v0[2] <= ub[2]); + return (v0[0] >= lb[0] && v0[0] <= ub[0] && + v0[1] >= lb[1] && v0[1] <= ub[1] && + v0[2] >= lb[2] && v0[2] <= ub[2]); } //returns the point position -Eigen::Vector3f Point::ClosestPoint(const Eigen::Vector3f& p) const +Eigen::Vector3f Point::ClosestPoint(const Eigen::Vector3f &p) const { return v0; } //returns the squared distance between the query point p and the current point -float Point::SqrDistance(const Eigen::Vector3f& p) const +float Point::SqrDistance(const Eigen::Vector3f &p) const { - Eigen::Vector3f d = p-ClosestPoint(p); + Eigen::Vector3f d = p - ClosestPoint(p); return d.squaredNorm(); } //returns the euclidean distance between the query point p and the current point -float Point::Distance(const Eigen::Vector3f& p) const +float Point::Distance(const Eigen::Vector3f &p) const { return sqrt(SqrDistance(p)); } diff --git a/exercise4/src/Triangle.cpp b/exercise4/src/Triangle.cpp index bb2a6d9..c02b97a 100644 --- a/exercise4/src/Triangle.cpp +++ b/exercise4/src/Triangle.cpp @@ -1,22 +1,21 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved #include "Triangle.h" #include "GridUtils.h" #include - //default constructor Triangle::Triangle() { } //constructs a triangle using the vertex positions v0,v1 and v2 -Triangle::Triangle(const Eigen::Vector3f& v0, const Eigen::Vector3f& v1,const Eigen::Vector3f& v2): v0(v0),v1(v1),v2(v2) +Triangle::Triangle(const Eigen::Vector3f &v0, const Eigen::Vector3f &v1, const Eigen::Vector3f &v2) : v0(v0), v1(v1), v2(v2) { } //constructs a triangle from the face f of the given halfedge mesh m -Triangle::Triangle(const HEMesh&m, const OpenMesh::FaceHandle& f):h(f) +Triangle::Triangle(const HEMesh &m, const OpenMesh::FaceHandle &f) : h(f) { OpenMesh::HalfedgeHandle he = m.halfedge_handle(f); v0 = ToEigenVector(m.point(m.from_vertex_handle(he))); @@ -29,66 +28,64 @@ Triangle::Triangle(const HEMesh&m, const OpenMesh::FaceHandle& f):h(f) Box Triangle::ComputeBounds() const { /* Task 3.2.2 */ - Box b; + Box b; return b; } - //returns true if the triangle overlaps the given box b -bool Triangle::Overlaps(const Box& b) const +bool Triangle::Overlaps(const Box &b) const { /* Task 3.2.2 */ //carefully look at the interface of the box class, there might be a lot of useful helper functions - return true; + return true; } //returns the barycentric coordinates of the point with the smallest distance to point p which lies on the triangle -void Triangle::ClosestPointBarycentric(const Eigen::Vector3f& p, float& l0, float& l1, float& l2) const +void Triangle::ClosestPointBarycentric(const Eigen::Vector3f &p, float &l0, float &l1, float &l2) const { Eigen::Vector3f edge0 = v1 - v0; Eigen::Vector3f edge1 = v2 - v0; Eigen::Vector3f v = v0 - p; - float a = edge0.dot( edge0 ); - float b = edge0.dot( edge1 ); - float c = edge1.dot( edge1 ); - float d = edge0.dot( v ); - float e = edge1.dot( v ); + float a = edge0.dot(edge0); + float b = edge0.dot(edge1); + float c = edge1.dot(edge1); + float d = edge0.dot(v); + float e = edge1.dot(v); - float det = a*c - b*b; - float s = b*e - c*d; - float t = b*d - a*e; + float det = a * c - b * b; + float s = b * e - c * d; + float t = b * d - a * e; - if ( s + t < det ) + if (s + t < det) { - if ( s < 0.f ) + if (s < 0.f) { - if ( t < 0.f ) + if (t < 0.f) { - if ( d < 0.f ) + if (d < 0.f) { - s=-d/a; - s=std::min(std::max(s,0.0f),1.0f); + s = -d / a; + s = std::min(std::max(s, 0.0f), 1.0f); t = 0.f; } else { s = 0.f; - t = -e/c; - t = std::min(std::max(t,0.0f),1.0f); - + t = -e / c; + t = std::min(std::max(t, 0.0f), 1.0f); } } else { s = 0.f; - t = -e/c; - t = std::min(std::max(t,0.0f),1.0f); + t = -e / c; + t = std::min(std::max(t, 0.0f), 1.0f); } } - else if ( t < 0.f ) + else if (t < 0.f) { - s = -d/a; - s=std::min(std::max(s,0.0f),1.0f); + s = -d / a; + s = std::min(std::max(s, 0.0f), 1.0f); t = 0.f; } else @@ -100,81 +97,77 @@ void Triangle::ClosestPointBarycentric(const Eigen::Vector3f& p, float& l0, floa } else { - if ( s < 0.f ) + if (s < 0.f) { - float tmp0 = b+d; - float tmp1 = c+e; - if ( tmp1 > tmp0 ) + float tmp0 = b + d; + float tmp1 = c + e; + if (tmp1 > tmp0) { float numer = tmp1 - tmp0; - float denom = a-2*b+c; - s = numer/denom; - s=std::min(std::max(s,0.0f),1.0f); - t = 1-s; + float denom = a - 2 * b + c; + s = numer / denom; + s = std::min(std::max(s, 0.0f), 1.0f); + t = 1 - s; } else { - t = -e/c; - t=std::min(std::max(t,0.0f),1.0f); + t = -e / c; + t = std::min(std::max(t, 0.0f), 1.0f); s = 0.f; } } - else if ( t < 0.f ) + else if (t < 0.f) { - if ( a+d > b+e ) + if (a + d > b + e) { - float numer = c+e-b-d; - float denom = a-2*b+c; - s = numer/denom; - s=std::min(std::max(s,0.0f),1.0f); - - t = 1-s; + float numer = c + e - b - d; + float denom = a - 2 * b + c; + s = numer / denom; + s = std::min(std::max(s, 0.0f), 1.0f); + + t = 1 - s; } else { - s = -e/c; - s=std::min(std::max(s,0.0f),1.0f); + s = -e / c; + s = std::min(std::max(s, 0.0f), 1.0f); t = 0.f; } } else { - float numer = c+e-b-d; - float denom = a-2*b+c; + float numer = c + e - b - d; + float denom = a - 2 * b + c; - s = numer/denom; - s=std::min(std::max(s,0.0f),1.0f); + s = numer / denom; + s = std::min(std::max(s, 0.0f), 1.0f); t = 1.f - s; } } - l0 = 1-s-t; + l0 = 1 - s - t; l1 = s; l2 = t; } //returns the point with smallest distance to point p which lies on the triangle -Eigen::Vector3f Triangle::ClosestPoint(const Eigen::Vector3f& p) const +Eigen::Vector3f Triangle::ClosestPoint(const Eigen::Vector3f &p) const { - float l0,l1,l2; - ClosestPointBarycentric(p,l0,l1,l2); - return l0*v0 + l1*v1 +l2* v2; - + float l0, l1, l2; + ClosestPointBarycentric(p, l0, l1, l2); + return l0 * v0 + l1 * v1 + l2 * v2; } //returns the squared distance between point p and the triangle -float Triangle::SqrDistance(const Eigen::Vector3f& p) const +float Triangle::SqrDistance(const Eigen::Vector3f &p) const { - Eigen::Vector3f d = p-ClosestPoint(p); + Eigen::Vector3f d = p - ClosestPoint(p); return d.squaredNorm(); } //returns the euclidean distance between point p and the triangle -float Triangle::Distance(const Eigen::Vector3f& p) const +float Triangle::Distance(const Eigen::Vector3f &p) const { return sqrt(SqrDistance(p)); } //returns a reference point which is on the triangle and is used to sort the primitive in the AABB tree construction Eigen::Vector3f Triangle::ReferencePoint() const { - return (v0+v1+v2)/3.0f; + return (v0 + v1 + v2) / 3.0f; } - - - diff --git a/exercise4/src/Viewer.cpp b/exercise4/src/Viewer.cpp index e0f22e7..e7ad51d 100644 --- a/exercise4/src/Viewer.cpp +++ b/exercise4/src/Viewer.cpp @@ -1,5 +1,5 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved #include "Viewer.h" @@ -24,12 +24,12 @@ Viewer::Viewer() : AbstractViewer("CG1 Exercise 3"), - renderer(polymesh), - closestPositions(nse::gui::VertexBuffer), - gridPositions(nse::gui::VertexBuffer), - rayPositions(nse::gui::VertexBuffer), rayCellsPositions(nse::gui::VertexBuffer) -{ - SetupGUI(); + renderer(polymesh), + closestPositions(nse::gui::VertexBuffer), + gridPositions(nse::gui::VertexBuffer), + rayPositions(nse::gui::VertexBuffer), rayCellsPositions(nse::gui::VertexBuffer) +{ + SetupGUI(); closestVAO.generate(); gridVAO.generate(); @@ -39,7 +39,7 @@ Viewer::Viewer() void Viewer::SetupGUI() { - auto mainWindow = SetupMainWindow(); + auto mainWindow = SetupMainWindow(); auto loadFileBtn = new nanogui::Button(mainWindow, "Load Mesh"); loadFileBtn->setCallback([this]() { @@ -52,39 +52,42 @@ void Viewer::SetupGUI() if (!OpenMesh::IO::read_mesh(polymesh, file)) { new nanogui::MessageDialog(this, nanogui::MessageDialog::Type::Warning, "Load Mesh", - "The specified file could not be loaded"); + "The specified file could not be loaded"); } else MeshUpdated(); } - }); + }); - cmbPrimitiveType = new nanogui::ComboBox(mainWindow, { "Use Vertices", "Use Edges", "Use Triangles" }); + cmbPrimitiveType = new nanogui::ComboBox(mainWindow, {"Use Vertices", "Use Edges", "Use Triangles"}); cmbPrimitiveType->setCallback([this](int) { FindClosestPoint(sldQuery->Value()); BuildGridVBO(); }); - - sldQuery = new nse::gui::VectorInput(mainWindow, "Query", Eigen::Vector3f::Zero(), Eigen::Vector3f::Zero(), Eigen::Vector3f::Zero(), [this](const Eigen::Vector3f& p) { FindClosestPoint(p); }); - sldRayOrigin = new nse::gui::VectorInput(mainWindow, "Ray Origin", Eigen::Vector3f::Zero(), Eigen::Vector3f::Zero(), Eigen::Vector3f::Zero(), [this](const Eigen::Vector3f& p) { BuildRayVBOs(); }); - sldRayDir = new nse::gui::VectorInput(mainWindow, "Ray Direction", Eigen::Vector3f::Constant(-1), Eigen::Vector3f::Constant(1), Eigen::Vector3f::Zero(), [this](const Eigen::Vector3f& p) { BuildRayVBOs(); }); - nanogui::TextBox* txtRaySteps; + sldQuery = new nse::gui::VectorInput(mainWindow, "Query", Eigen::Vector3f::Zero(), Eigen::Vector3f::Zero(), Eigen::Vector3f::Zero(), [this](const Eigen::Vector3f &p) { FindClosestPoint(p); }); + + sldRayOrigin = new nse::gui::VectorInput(mainWindow, "Ray Origin", Eigen::Vector3f::Zero(), Eigen::Vector3f::Zero(), Eigen::Vector3f::Zero(), [this](const Eigen::Vector3f &p) { BuildRayVBOs(); }); + sldRayDir = new nse::gui::VectorInput(mainWindow, "Ray Direction", Eigen::Vector3f::Constant(-1), Eigen::Vector3f::Constant(1), Eigen::Vector3f::Zero(), [this](const Eigen::Vector3f &p) { BuildRayVBOs(); }); + nanogui::TextBox *txtRaySteps; auto sldRaySteps = nse::gui::AddLabeledSlider(mainWindow, "Ray Steps", std::make_pair(1, 200), 80, txtRaySteps); sldRaySteps->setCallback([this, txtRaySteps](float value) { raySteps = (int)std::round(value); txtRaySteps->setValue(std::to_string(raySteps)); BuildRayVBOs(); }); - sldRaySteps->callback()(sldRaySteps->value()); + sldRaySteps->callback()(sldRaySteps->value()); - chkRenderMesh = new nanogui::CheckBox(mainWindow, "Render Mesh"); chkRenderMesh->setChecked(true); - chkRenderGrid = new nanogui::CheckBox(mainWindow, "Render Non-Empty Grid Cells"); chkRenderGrid->setChecked(false); - chkRenderRay = new nanogui::CheckBox(mainWindow, "Render Ray"); chkRenderRay->setChecked(false); + chkRenderMesh = new nanogui::CheckBox(mainWindow, "Render Mesh"); + chkRenderMesh->setChecked(true); + chkRenderGrid = new nanogui::CheckBox(mainWindow, "Render Non-Empty Grid Cells"); + chkRenderGrid->setChecked(false); + chkRenderRay = new nanogui::CheckBox(mainWindow, "Render Ray"); + chkRenderRay->setChecked(false); - shadingBtn = new nanogui::ComboBox(mainWindow, { "Smooth Shading", "Flat Shading" }); + shadingBtn = new nanogui::ComboBox(mainWindow, {"Smooth Shading", "Flat Shading"}); performLayout(); } -void Viewer::FindClosestPoint(const Eigen::Vector3f& p) +void Viewer::FindClosestPoint(const Eigen::Vector3f &p) { if (polymesh.vertices_empty()) return; @@ -101,7 +104,7 @@ void Viewer::FindClosestPoint(const Eigen::Vector3f& p) case Tri: closest = triangleTree.ClosestPoint(p); break; - } + } auto timeEnd = std::chrono::high_resolution_clock::now(); std::cout << std::fixed << "Closest point query took " << std::chrono::duration_cast(timeEnd - timeStart).count() << " microseconds." << std::endl; @@ -109,7 +112,7 @@ void Viewer::FindClosestPoint(const Eigen::Vector3f& p) points.block<3, 1>(0, 0) = p; points.block<3, 1>(0, 1) = closest; points.row(3).setConstant(1); - + closestVAO.bind(); ShaderPool::Instance()->simpleShader.bind(); closestPositions.uploadData(points).bindToAttribute("position"); @@ -122,11 +125,11 @@ void Viewer::MeshUpdated() nse::math::BoundingBox bbox; for (auto v : polymesh.vertices()) bbox.expand(ToEigenVector(polymesh.point(v))); - camera().FocusOnBBox(bbox); + camera().FocusOnBBox(bbox); bboxMaxLength = bbox.diagonal().maxCoeff(); polymesh.triangulate(); - + BuildAABBTreeFromVertices(polymesh, vertexTree); BuildAABBTreeFromEdges(polymesh, edgeTree); BuildAABBTreeFromTriangles(polymesh, triangleTree); @@ -134,7 +137,7 @@ void Viewer::MeshUpdated() Eigen::Vector3f cellSize = Eigen::Vector3f::Constant(bbox.diagonal().maxCoeff() / 50); BuildHashGridFromVertices(polymesh, vertexGrid, cellSize); BuildHashGridFromEdges(polymesh, edgeGrid, cellSize); - BuildHashGridFromTriangles(polymesh, triangleGrid, cellSize); + BuildHashGridFromTriangles(polymesh, triangleGrid, cellSize); sldQuery->SetBounds(bbox.min, bbox.max); sldQuery->SetValue(bbox.max); @@ -145,7 +148,7 @@ void Viewer::MeshUpdated() sldRayDir->SetValue(bbox.diagonal().normalized()); BuildGridVBO(); - + renderer.Update(); } @@ -196,14 +199,18 @@ void Viewer::BuildRayVBOs() rayCellsIndices = (GLuint)cellPositions.size(); } -void Viewer::AddBoxVertices(const Box & box, std::vector& positions) +void Viewer::AddBoxVertices(const Box &box, std::vector &positions) { - auto& lb = box.LowerBound(); - auto& ub = box.UpperBound(); - Eigen::Vector4f o; o << lb, 1.0f; - Eigen::Vector4f x; x << ub.x() - lb.x(), 0, 0, 0; - Eigen::Vector4f y; y << 0, ub.y() - lb.y(), 0, 0; - Eigen::Vector4f z; z << 0, 0, ub.z() - lb.z(), 0; + auto &lb = box.LowerBound(); + auto &ub = box.UpperBound(); + Eigen::Vector4f o; + o << lb, 1.0f; + Eigen::Vector4f x; + x << ub.x() - lb.x(), 0, 0, 0; + Eigen::Vector4f y; + y << 0, ub.y() - lb.y(), 0, 0; + Eigen::Vector4f z; + z << 0, 0, ub.z() - lb.z(), 0; positions.push_back(o); positions.push_back(o + x); positions.push_back(o + x); @@ -242,14 +249,14 @@ void Viewer::drawContents() camera().ComputeCameraMatrices(view, proj); Eigen::Matrix4f mvp = proj * view; - if(chkRenderMesh->checked()) + if (chkRenderMesh->checked()) renderer.Render(view, proj, shadingBtn->selectedIndex() == 1); ShaderPool::Instance()->simpleShader.bind(); ShaderPool::Instance()->simpleShader.setUniform("mvp", mvp); //Draw line between query point and its closest position - closestVAO.bind(); + closestVAO.bind(); ShaderPool::Instance()->simpleShader.setUniform("color", Eigen::Vector4f(1, 1, 1, 1)); glDrawArrays(GL_LINES, 0, 2); ShaderPool::Instance()->simpleShader.setUniform("color", Eigen::Vector4f(1, 0, 0, 1)); diff --git a/exercise4/src/main.cpp b/exercise4/src/main.cpp index e94f898..9555b3b 100644 --- a/exercise4/src/main.cpp +++ b/exercise4/src/main.cpp @@ -1,5 +1,5 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved #include @@ -8,13 +8,13 @@ #include "Viewer.h" -int main(int argc, char* argv[]) +int main(int argc, char *argv[]) { std::cout.imbue(std::locale("")); nanogui::init(); - { + { nanogui::ref viewer = new Viewer(); viewer->setVisible(true); @@ -25,11 +25,10 @@ int main(int argc, char* argv[]) { nanogui::mainloop(); } - catch (std::runtime_error& e) + catch (std::runtime_error &e) { std::cerr << e.what() << std::endl; } - } nanogui::shutdown();