Commit b80341d9 authored by Sylvain Thery's avatar Sylvain Thery

add BB in OBJ import struct

parent 32c430ec
......@@ -28,6 +28,7 @@
#include "Container/containerBrowser.h"
#include "Topology/generic/cellmarker.h"
#include "Utils/textures.h"
#include "Geometry/bounding_box.h"
namespace CGoGN
{
......@@ -92,13 +93,13 @@ protected:
std::vector<unsigned int> m_beginIndices;
std::vector<unsigned int> m_nbIndices;
std::vector<unsigned int> m_groupIdx;
unsigned int m_maxTextureSize;
std::vector<unsigned int> m_objGroups;
std::vector<std::string> m_groupNames;
std::vector< Geom::BoundingBox<VEC3> > m_groupBBs;
/// vector of group name
// std::vector<std::string> m_groupNames;
// std::vector<std::string> m_groupMaterialNames;
// std::vector<int> m_groupMaterialID;
unsigned int m_maxTextureSize;
/// map of material names -> group id
std::map<std::string,int> m_materialNames;
......@@ -121,6 +122,9 @@ protected:
unsigned int m_tagG ;
unsigned int m_tagF ;
void updateGroups(const std::vector<Geom::Vec3f>& pos);
public:
/// marker for special vertices (with several normals & tex coords)
......@@ -268,6 +272,30 @@ public:
*/
unsigned int nbIndices(unsigned int i) const { return m_nbIndices[i]; }
/**
* @brief get the id of group in OBJ file
* @param i id of group
* @return obj group index
*/
unsigned int groupIdx(unsigned int i) const { return m_groupIdx[i]; }
/**
* @brief get the number of groups in OBJ file
* @return number of groups
*/
unsigned int nbObjGroups() { return m_objGroups.size()-1; }
/**
* @brief get the index of first group mat of obj
* @param i id of obj group
* @return id of first group mat
*/
unsigned int objGroup(unsigned int i) const { return m_objGroups[i]; }
const Geom::BoundingBox<VEC3>& getGroupBB(unsigned int i) const { return m_groupBBs[i];}
const std::string& objGroupName(unsigned int i) const { return m_groupNames[i];}
/**
* @brief create simple VBO for separated triangles
* @param positionVBO
......
......@@ -653,13 +653,18 @@ bool OBJModel<PFP>::createGroupMatVBO_P(Utils::VBO* positionVBO)
unsigned int sz = group_faces.size();
m_beginIndices.resize(sz);
m_nbIndices.resize(sz);
m_groupIdx.resize(sz);
for (unsigned int g=0; g<sz; ++g)
{
unsigned int nbtris = 0;
std::vector<Dart>& traf = group_faces[g];
if (m_tagG != 0)
m_groupIdx[g] = m_groups[traf.front()];
else
m_groupIdx[g]=0;
for (std::vector<Dart>::iterator id=traf.begin(); id!= traf.end(); ++id)
{
Dart d = *id;
......@@ -685,6 +690,8 @@ bool OBJModel<PFP>::createGroupMatVBO_P(Utils::VBO* positionVBO)
memcpy(ptrPos,&posBuff[0],posBuff.size()*sizeof(Geom::Vec3f));
positionVBO->releasePtr();
updateGroups();
return true;
}
......@@ -714,13 +721,18 @@ bool OBJModel<PFP>::createGroupMatVBO_PT(Utils::VBO* positionVBO, Utils::VBO* te
unsigned int sz = group_faces.size();
m_beginIndices.resize(sz);
m_nbIndices.resize(sz);
m_groupIdx.resize(sz);
for (unsigned int g=0; g<sz; ++g)
{
unsigned int nbtris = 0;
std::vector<Dart>& traf = group_faces[g];
if (m_tagG != 0)
m_groupIdx[g] = m_groups[traf.front()];
else
m_groupIdx[g]=0;
for (std::vector<Dart>::iterator id=traf.begin(); id!= traf.end(); ++id)
{
Dart d = *id;
......@@ -768,6 +780,8 @@ bool OBJModel<PFP>::createGroupMatVBO_PT(Utils::VBO* positionVBO, Utils::VBO* te
memcpy(ptrTC,&TCBuff[0],TCBuff.size()*sizeof(Geom::Vec2f));
texcoordVBO->releasePtr();
updateGroups();
return true;
}
......@@ -800,13 +814,18 @@ bool OBJModel<PFP>::createGroupMatVBO_PN(Utils::VBO* positionVBO, Utils::VBO* no
unsigned int sz = group_faces.size();
m_beginIndices.resize(sz);
m_nbIndices.resize(sz);
m_groupIdx.resize(sz);
for (unsigned int g=0; g<sz; ++g)
{
unsigned int nbtris = 0;
std::vector<Dart>& traf = group_faces[g];
if (m_tagG != 0)
m_groupIdx[g] = m_groups[traf.front()];
else
m_groupIdx[g]=0;
for (std::vector<Dart>::iterator id=traf.begin(); id!= traf.end(); ++id)
{
Dart d = *id;
......@@ -866,6 +885,8 @@ bool OBJModel<PFP>::createGroupMatVBO_PN(Utils::VBO* positionVBO, Utils::VBO* no
memcpy(ptrNormal, &normalBuff[0], normalBuff.size()*sizeof(Geom::Vec3f));
normalVBO->releasePtr();
updateGroups();
return true;
}
......@@ -913,12 +934,18 @@ bool OBJModel<PFP>::createGroupMatVBO_PTN( Utils::VBO* positionVBO,
m_beginIndices.resize(sz);
m_nbIndices.resize(sz);
m_groupIdx.resize(sz);
for (unsigned int g=0; g<sz; ++g)
{
unsigned int nbtris = 0;
std::vector<Dart>& traf = group_faces[g];
if (m_tagG != 0)
m_groupIdx[g] = m_groups[traf.front()];
else
m_groupIdx[g]=0;
for (std::vector<Dart>::iterator id=traf.begin(); id!= traf.end(); ++id)
{
Dart d = *id;
......@@ -1010,11 +1037,43 @@ bool OBJModel<PFP>::createGroupMatVBO_PTN( Utils::VBO* positionVBO,
memcpy(ptrNormal, &normalBuff[0], normalBuff.size()*sizeof(Geom::Vec3f));
normalVBO->releasePtr();
updateGroups(posBuff);
return true;
}
template <typename PFP>
void OBJModel<PFP>::updateGroups(const std::vector<Geom::Vec3f>& pos)
{
unsigned int sz = m_beginIndices.size();
unsigned int i=0;
do
{
m_objGroups.push_back(i);
m_groupBBs.resize(m_objGroups.size());
Geom::BoundingBox<VEC3>& bb = m_groupBBs.back();
unsigned int grp = m_groupIdx[i];
while ((i<sz) && (m_groupIdx[i]==grp))
{
// update BB
unsigned int beg = m_beginIndices[i];
unsigned int end = m_beginIndices[i] + m_nbIndices[i];
for (unsigned int j=beg; j!=end; ++j )
bb.addPoint(pos[j]);
++i;
}
bb.centeredScale(1.1);
}while (i<sz);
m_objGroups.push_back(sz);
}
template <typename PFP>
bool OBJModel<PFP>::import( const std::string& filename, std::vector<std::string>& attrNames)
{
......@@ -1043,7 +1102,7 @@ bool OBJModel<PFP>::import( const std::string& filename, std::vector<std::string
m_tagVN++;
if (tag == "vt")
m_tagVT++;
if (tag == "g")
if ((tag == "g") || (tag == "o"))
m_tagG++;
if (tag == "f")
m_tagF++;
......@@ -1094,7 +1153,7 @@ bool OBJModel<PFP>::import( const std::string& filename, std::vector<std::string
}
if (m_tagG != 0)
// if (m_tagG != 0) always use group even if not in the file
{
m_groups = m_map.template getAttribute<unsigned int, FACE>("groups") ;
if (!m_groups.isValid())
......@@ -1207,20 +1266,11 @@ bool OBJModel<PFP>::import( const std::string& filename, std::vector<std::string
}
}
// if (tag == std::string("g"))
// {
// m_groupNames.push_back(ligne);
// std::string buf;
// fp >> buf;
// if (buf != "usemtl")
// {
// CGoGNerr << "problem reading OBJ, waiting for usemtl get "<< buf << CGoGNendl;
// }
// fp >> buf;
// m_materialNames.insert(std::pair<std::string,int>(buf,-1));
// m_groupMaterialNames.push_back(buf);
// currentGroup++;
// }
if ( (tag == std::string("g")) && (tag == std::string("g")) )
{
m_groupNames.push_back(ligne);
currentGroup++;
}
if (tag == std::string("f"))
{
......
......@@ -104,6 +104,9 @@ class BoundingBox
// 0-centered scale of the bounding box
void centeredScale(typename VEC::DATA_TYPE size);
/// test if bb is intersected by a ray
bool rayIntersect(const VEC& P, const VEC& V) const;
/**********************************************/
/* STREAM OPERATORS */
/**********************************************/
......
......@@ -318,6 +318,51 @@ void BoundingBox<VEC>::centeredScale(typename VEC::DATA_TYPE size)
m_pMax = ((m_pMax - center) * size) + center ;
}
/// test if bb is intersected by a ray
template <typename VEC>
bool BoundingBox<VEC>::rayIntersect(const VEC& P, const VEC& V) const
{
static float EPSILON = 0.000001f;
if (fabs(V[2]) > EPSILON)
{
VEC Q = P + ((m_pMin[2] - P[2])/V[2])*V;
if ((Q[0]<m_pMax[0]) && (Q[0]>m_pMin[0]) && (Q[1]<m_pMax[1]) && (Q[1]>m_pMin[1]))
return true;
Q = P + ((m_pMax[2] - P[2])/V[2])*V;
if ((Q[0]<m_pMax[0]) && (Q[0]>m_pMin[0]) && (Q[1]<m_pMax[1]) && (Q[1]>m_pMin[1]))
return true;
}
if (fabs(V[1]) > EPSILON)
{
VEC Q = P + ((m_pMin[1] - P[1])/V[1])*V;
if ((Q[0]<m_pMax[0]) && (Q[0]>m_pMin[0]) && (Q[2]<m_pMax[2]) && (Q[2]>m_pMin[2]))
return true;
Q = P + ((m_pMax[1] - P[1])/V[1])*V;
if ((Q[0]<m_pMax[0]) && (Q[0]>m_pMin[0]) && (Q[2]<m_pMax[2]) && (Q[2]>m_pMin[2]))
return true;
}
if (fabs(V[0]) > EPSILON)
{
VEC Q = P + ((m_pMin[0] - P[0])/V[0])*V;
if ((Q[1]<m_pMax[1]) && (Q[1]>m_pMin[1]) && (Q[2]<m_pMax[2]) && (Q[2]>m_pMin[2]))
return true;
Q = P + ((m_pMax[0] - P[0])/V[0])*V;
if ((Q[1]<m_pMax[1]) && (Q[1]>m_pMin[1]) && (Q[2]<m_pMax[2]) && (Q[2]>m_pMin[2]))
return true;
}
return false;
}
} // namespace Geom
} // namespace CGoGN
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment