Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
O
openfpm_io
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
Requirements
Requirements
List
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Environments
Analytics
Analytics
CI / CD
Code Review
Insights
Issue
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openfpm
openfpm_io
Commits
0d13173f
Commit
0d13173f
authored
Nov 06, 2018
by
incardon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding ObjReader
parent
9f8da5bc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
208 additions
and
0 deletions
+208
-0
src/ObjReader/ObjReader.hpp
src/ObjReader/ObjReader.hpp
+188
-0
src/ObjReader/ObjReader_unit_test.cpp
src/ObjReader/ObjReader_unit_test.cpp
+20
-0
No files found.
src/ObjReader/ObjReader.hpp
0 → 100644
View file @
0d13173f
/*
* ObjReader.hpp
*
* Created on: Oct 31, 2018
* Author: i-bird
*/
#ifndef OBJREADER_HPP_
#define OBJREADER_HPP_
#include "config.h"
#include "Space/Shape/Point.hpp"
#ifdef HAVE_TINYOBJLOADER
#include "tiny_obj_loader.h"
/*! \brief Wavefront obj File reader
*
* \tparam T precision of the 3D space
*
*/
template
<
typename
T
>
class
ObjReader
{
size_t
shape_counter
;
size_t
face_counter
;
size_t
index_offset
;
tinyobj
::
attrib_t
attrib
;
std
::
vector
<
tinyobj
::
shape_t
>
shapes
;
std
::
vector
<
tinyobj
::
material_t
>
materials
;
size_t
end_shape
;
public:
/*! \brief Constructor
*
*
*/
ObjReader
()
{}
/*! \brief Read an Wavefront obj file
*
* \param file to open
*
* \return true if the read succeed
*/
bool
read
(
std
::
string
file
)
{
shape_counter
=
0
;
face_counter
=
0
;
index_offset
=
0
;
shapes
.
clear
();
materials
.
clear
();
std
::
string
err
;
bool
ret
=
tinyobj
::
LoadObj
(
&
attrib
,
&
shapes
,
&
materials
,
&
err
,
file
.
c_str
());
if
(
!
err
.
empty
())
{
std
::
cerr
<<
err
<<
std
::
endl
;}
if
(
!
ret
)
{
return
false
;}
end_shape
=
shapes
.
size
();
return
true
;
}
/*! \brief Next triangle
*
* \return itself
*
*/
ObjReader
&
operator
++
()
{
index_offset
+=
getFaceNVertex
();
++
face_counter
;
if
(
face_counter
>=
shapes
[
shape_counter
].
mesh
.
num_face_vertices
.
size
())
{
face_counter
=
0
;
index_offset
=
0
;
++
shape_counter
;
}
return
*
this
;
}
/*! \brief Return true if we have another face
*
* \return true if we have next face
*/
inline
bool
isNext
()
{
return
shape_counter
<
end_shape
;
}
/*! \brief Iterate only on a partucular object faces
*
* \param i object to iterate
*
* \return
*/
inline
void
setObject
(
int
i
)
{
if
(
i
>=
shapes
.
size
())
{
std
::
cerr
<<
"Error "
<<
__FILE__
<<
":"
<<
__LINE__
<<
" you selected object "
<<
i
<<
" but the file contain "
<<
shapes
.
size
()
<<
" objects"
<<
std
::
endl
;
}
shape_counter
=
i
;
end_shape
=
i
+
1
;
}
/*! \brief Get the number of vertices in the face
*
* \return number of vertices a face has
*/
unsigned
int
getFaceNVertex
()
{
return
shapes
[
shape_counter
].
mesh
.
num_face_vertices
[
face_counter
];
}
/*! \brief return the vertex v of the actual face
*
* \param v vertex
* \return the position of such vertex
*/
inline
Point
<
3
,
T
>
getVertex
(
unsigned
int
v
)
{
Point
<
3
,
T
>
p
;
tinyobj
::
index_t
idx
=
shapes
[
shape_counter
].
mesh
.
indices
[
index_offset
+
v
];
p
.
get
(
0
)
=
attrib
.
vertices
[
3
*
idx
.
vertex_index
+
0
];
p
.
get
(
1
)
=
attrib
.
vertices
[
3
*
idx
.
vertex_index
+
1
];
p
.
get
(
2
)
=
attrib
.
vertices
[
3
*
idx
.
vertex_index
+
2
];
return
p
;
}
/*! \brief return the vertex v of the actual face
*
* \param v vertex
* \return the position of such vertex
*/
inline
Point
<
3
,
T
>
getNormal
(
unsigned
int
v
)
{
Point
<
3
,
T
>
p
;
tinyobj
::
index_t
idx
=
shapes
[
shape_counter
].
mesh
.
indices
[
index_offset
+
v
];
p
.
get
(
0
)
=
attrib
.
normals
[
3
*
idx
.
normal_index
+
0
];
p
.
get
(
1
)
=
attrib
.
normals
[
3
*
idx
.
normal_index
+
1
];
p
.
get
(
2
)
=
attrib
.
normals
[
3
*
idx
.
normal_index
+
2
];
return
p
;
}
/*! \brief Get the texture coordinates
*
* \param v vertex
*
* \return The texture coordinates of the vertex v
*
*/
inline
Point
<
2
,
T
>
getTexCoord
(
unsigned
int
v
)
{
Point
<
2
,
T
>
p
;
tinyobj
::
index_t
idx
=
shapes
[
shape_counter
].
mesh
.
indices
[
index_offset
+
v
];
p
.
get
(
0
)
=
attrib
.
texcoords
[
2
*
idx
.
texcoord_index
+
0
];
p
.
get
(
1
)
=
attrib
.
texcoords
[
2
*
idx
.
texcoord_index
+
1
];
return
p
;
}
};
#endif
#endif
/* OBJREADER_HPP_ */
src/ObjReader/ObjReader_unit_test.cpp
0 → 100644
View file @
0d13173f
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include "ObjReader.hpp"
#ifdef HAVE_TINYOBJLOADER
BOOST_AUTO_TEST_SUITE
(
obj_reader_test_suite
)
BOOST_AUTO_TEST_CASE
(
obj_reader_test_use
)
{
ObjReader
<
float
>
reader
;
reader
.
read
(
""
);
}
BOOST_AUTO_TEST_SUITE_END
()
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment