Skip to content

Commit

Permalink
Merge pull request #10 from Danny02/feature/valette-master
Browse files Browse the repository at this point in the history
add vtk feature from valette
  • Loading branch information
Danny02 committed Mar 14, 2018
2 parents d8a213e + d1c7a05 commit 243a343
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 30 deletions.
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

sudo: false

addons:
apt:
packages:
- libgtk2.0-dev
- freeglut3-dev

language: cpp

script:
- make -f Makefile.linux
- ./build-src.sh
55 changes: 34 additions & 21 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
cmake_minimum_required(VERSION 2.6)
cmake_policy(SET CMP0012 NEW)
cmake_minimum_required(VERSION 2.8)
project(vtkOpenCTM)

# Workaround target_compile_options for cmake < 2.8.12
if(${CMAKE_VERSION} VERSION_LESS "2.8.12")
function(target_compile_options target scope)
set(args ${ARGV})
list(REMOVE_AT args 0 1)
string(REPLACE ";" " " CFLAGS_STRING "${args}")
set_target_properties(${target} PROPERTIES COMPILE_FLAGS "${CFLAGS_STRING}")
endfunction()
endif()
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})

INCLUDE_DIRECTORIES(lib)
INCLUDE_DIRECTORIES(lib/liblzma)
INCLUDE_DIRECTORIES(tools/rply)
INCLUDE_DIRECTORIES(tools/tinyxml)

project(OpenCTM)
AUX_SOURCE_DIRECTORY(lib ctm)
AUX_SOURCE_DIRECTORY(lib/liblzma lzma)
AUX_SOURCE_DIRECTORY(tools/rply rply)
AUX_SOURCE_DIRECTORY(tools/tinyxml tinyxml)

option(BUILD_TOOLSET "Build tools: ctmconv, ctmviewer" ON)
option(BUILD_DOCUMENTATION "Build documentation: manpages" ON)
ADD_DEFINITIONS (-D VTKINCLUDED)

ADD_EXECUTABLE(mesh2ctm
tools/ctmconv.cpp
tools/convoptions.cpp
tools/systimer.cpp
tools/mesh.cpp
tools/meshio.cpp
tools/ctm.cpp
tools/meshio.cpp
tools/ply.cpp
tools/stl.cpp
tools/3ds.cpp
tools/dae.cpp
tools/lwo.cpp
tools/obj.cpp
tools/off.cpp
tools/wrl.cpp
tools/common.cpp
${ctm} ${lzma} ${rply} ${tinyxml}
)
TARGET_LINK_LIBRARIES(mesh2ctm ${VTK_LIBRARIES})

add_subdirectory(lib)
if(${BUILD_TOOLSET})
add_subdirectory(tools)
endif()
if(${BUILD_DOCUMENTATION})
add_subdirectory(doc)
endif()
13 changes: 5 additions & 8 deletions README.txt → README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
1. INTRODUCTION
===============
# 1. INTRODUCTION

Welcome to OpenCTM!

Expand All @@ -11,15 +10,14 @@ through a simple, portable API.
The library is written in portable C (C99), and should compile nicely on any
32/64-bit system regardless of endianity (big endian or little endian).

[![Build Status](https://travis-ci.org/Danny02/OpenCTM.svg?branch=master)](https://travis-ci.org/Danny02/OpenCTM)

2. LICENSE
==========
# 2. LICENSE

The OpenCTM API and the OpenCTM tools are released under the zlib/libpng
license (see LICENSE.txt).

3. CREDITS
==========
# 3. CREDITS

Many people have helped out in the development process of OpenCTM, with
valuable feedback, programming efforts, test models and conceptual ideas.
Expand Down Expand Up @@ -52,8 +50,7 @@ Legal notices:
- This software is based in part on the work of the Independent JPEG Group.


4. CHANGES
==========
# 4. CHANGES

v1.0.3 - 2010.01.15
-------------------
Expand Down
6 changes: 6 additions & 0 deletions tools/convoptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Options::Options()
mNormalPrecision = 1.0f / 256.0f;
mTexMapPrecision = 1.0f / 4096.0f;
mColorPrecision = 1.0f / 256.0f;
mAttributePrecision = 1.0f / 256.0f;
mComment = string("");
mTexFileName = string("");
}
Expand Down Expand Up @@ -174,6 +175,11 @@ void Options::GetFromArgs(int argc, char **argv, int aStartIdx)
mColorPrecision = GetFloatArg(argv[i + 1]);
++ i;
}
else if((cmd == string("--aprec")) && (i < (argc - 1)))
{
mAttributePrecision = GetFloatArg(argv[i + 1]);
++ i;
}
else if((cmd == string("--comment")) && (i < (argc - 1)))
{
mComment = string(argv[i + 1]);
Expand Down
1 change: 1 addition & 0 deletions tools/convoptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Options {
CTMfloat mNormalPrecision;
CTMfloat mTexMapPrecision;
CTMfloat mColorPrecision;
CTMfloat mAttributePrecision;

std::string mComment;
std::string mTexFileName;
Expand Down
7 changes: 7 additions & 0 deletions tools/ctm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ void Export_CTM(const char * aFileName, Mesh * aMesh, Options &aOptions)
ctm.AttribPrecision(map, aOptions.mColorPrecision);
}

// Define custom attributes
if(aMesh->HasAttributes())
{
CTMenum map = ctm.AddAttribMap(&aMesh->mAttributes[0].x, aMesh->attributesName);
ctm.AttribPrecision(map, aOptions.mAttributePrecision);
}

// Set file comment
if(aMesh->mComment.size() > 0)
ctm.FileComment(aMesh->mComment.c_str());
Expand Down
2 changes: 2 additions & 0 deletions tools/ctmconv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ int main(int argc, char ** argv)
cout << " --nprec arg Set normal precision" << endl;
cout << " --tprec arg Set texture map precision" << endl;
cout << " --cprec arg Set color precision" << endl;
cout << " --aprec arg Set attributes precision" << endl;
cout << endl << " Miscellaneous" << endl;
cout << " --comment arg Set the file comment (default is to use the comment" << endl;
cout << " from the input file, if any)." << endl;
Expand Down Expand Up @@ -228,6 +229,7 @@ int main(int argc, char ** argv)
catch(exception &e)
{
cout << "Error: " << e.what() << endl;
return 1;
}

return 0;
Expand Down
10 changes: 10 additions & 0 deletions tools/mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class Mesh {
Mesh()
{
mOriginalNormals = true;
attributesName = 0;
}

/// Clear the mesh
Expand Down Expand Up @@ -172,14 +173,23 @@ class Mesh {
return (mTexCoords.size() > 0) && (mTexCoords.size() == mVertices.size());
}

/// Check if the mesh has custom attributes
bool HasAttributes()
{
return (mAttributes.size() > 0) && (mAttributes.size() == mVertices.size());
}

std::string mComment;
std::string mTexFileName;
std::vector<int> mIndices;
std::vector<Vector3> mVertices;
std::vector<Vector3> mNormals;
std::vector<Vector4> mColors;
std::vector<Vector4> mAttributes;
std::vector<Vector2> mTexCoords;

char *attributesName;

private:
/// Automatic detection of the optimal normal calculation method
NormalCalcAlgo DetectNormalCalculationMethod();
Expand Down
4 changes: 4 additions & 0 deletions tools/meshio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "obj.h"
#include "lwo.h"
#include "off.h"
#include "vtk.h"
#include "wrl.h"
#include "common.h"

Expand Down Expand Up @@ -67,6 +68,8 @@ void ImportMesh(const char * aFileName, Mesh * aMesh)
Import_OFF(aFileName, aMesh);
else if(fileExt == string(".WRL"))
Import_WRL(aFileName, aMesh);
else if(fileExt == string(".VTK"))
Import_VTK(aFileName, aMesh);
else
throw runtime_error("Unknown input file extension.");
}
Expand Down Expand Up @@ -109,4 +112,5 @@ void SupportedFormats(list<string> &aList)
aList.push_back(string("LightWave object (.lwo)"));
aList.push_back(string("Geomview object file format (.off)"));
aList.push_back(string("VRML 2.0 (.wrl) - export only"));
aList.push_back(string("VTK (.vtk) - import only"));
}
51 changes: 50 additions & 1 deletion tools/stl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
#include <algorithm>
#include "stl.h"

#ifdef VTKINCLUDED

#include <vtkPointData.h>
#include <vtkPolyData.h>
#include <vtkSTLReader.h>

#endif

#ifdef _MSC_VER
typedef unsigned int uint32;
#else
Expand Down Expand Up @@ -105,13 +113,51 @@ class SortVertex {
return (x < v.x) || ((x == v.x) && ((y < v.y) || ((y == v.y) && (z < v.z))));
}
};

/// Import an STL file from a file.
void Import_STL(const char * aFileName, Mesh * aMesh)
{
// Clear the mesh
aMesh->Clear();

#ifdef VTKINCLUDED
// Open the STL file
vtkSTLReader *reader = vtkSTLReader::New();
reader->SetFileName(aFileName);
reader->Update();

vtkPolyData *mesh = reader->GetOutput();

int faceCount = mesh->GetNumberOfCells();
int vertexCount = mesh->GetNumberOfPoints();

double p[3];
for (int i = 0; i < mesh->GetNumberOfPoints(); i++) {
mesh->GetPoint(i, p);
aMesh->mVertices.push_back(Vector3(p[0], p[1], p[2]));
}

vtkIdType *vertices, nVertices;
mesh->BuildCells();
for (int i = 0; i < mesh->GetNumberOfCells(); i++) {
mesh->GetCellPoints(i, nVertices, vertices);
for (int j = 0; j < 3; j++) {
aMesh->mIndices.push_back(vertices[j]);
}
}

vtkDataArray *pointData = mesh->GetPointData()->GetScalars();
if (!pointData) {
return;
}

aMesh->attributesName = pointData->GetName();

for (int i = 0; i < mesh->GetNumberOfPoints(); i++) {
aMesh->mAttributes.push_back(Vector4(pointData->GetTuple1(i), 0, 0, 1));
}

#else

// Open the input file
ifstream f(aFileName, ios::in | ios::binary);
if(f.fail())
Expand Down Expand Up @@ -184,6 +230,9 @@ void Import_STL(const char * aFileName, Mesh * aMesh)

// Close the input file
f.close();

#endif

}

/// Export an STL file to a file.
Expand Down
74 changes: 74 additions & 0 deletions tools/vtk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//-----------------------------------------------------------------------------
// Product: OpenCTM tools
// File: vtk.h
// Description: Interface for the vtk file format importer/exporter.
//-----------------------------------------------------------------------------

#ifndef __VTK_H_
#define __VTK_H_

#include <stdlib.h>
#include <iostream>
#include "mesh.h"
#include "convoptions.h"

#ifdef VTKINCLUDED

#include <vtkPointData.h>
#include <vtkPolyData.h>
#include <vtkPolyDataReader.h>

/// Import a VTK file from a file.
void Import_VTK(const char * aFileName, Mesh * aMesh) {
// Clear the mesh
aMesh->Clear();

// Open the VTK file
vtkPolyDataReader *reader = vtkPolyDataReader::New();
reader->SetFileName(aFileName);
reader->Update();

vtkPolyData *mesh = reader->GetOutput();

int faceCount = mesh->GetNumberOfCells();
int vertexCount = mesh->GetNumberOfPoints();

double p[3];
for (int i = 0; i < mesh->GetNumberOfPoints(); i++) {
mesh->GetPoint(i, p);
aMesh->mVertices.push_back(Vector3(p[0], p[1], p[2]));
}

vtkIdType *vertices, nVertices;
mesh->BuildCells();
for (int i = 0; i < mesh->GetNumberOfCells(); i++) {
mesh->GetCellPoints(i, nVertices, vertices);
for (int j = 0; j < 3; j++) {
aMesh->mIndices.push_back(vertices[j]);
}
}

vtkDataArray *pointData = mesh->GetPointData()->GetScalars();
if (!pointData) {
return;
}

aMesh->attributesName = pointData->GetName();

for (int i = 0; i < mesh->GetNumberOfPoints(); i++) {
aMesh->mAttributes.push_back(Vector4(pointData->GetTuple1(i), 0, 0, 1));
}


}
#else

/// Import a VTK file from a file.
void Import_VTK(const char * aFileName, Mesh * aMesh) {
std::cout << "error : vtk is not included in this library" << std::endl;
exit(1);
}

#endif

#endif // __PLY_H_

0 comments on commit 243a343

Please sign in to comment.