ter_meshes.cpp 5.57 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25 26 27 28
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <iostream>

#include "Utils/glutwin.h"

29
#include "Topology/generic/parameters.h"
30
#include "Topology/map/embeddedMap2.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

#include "Geometry/vector_gen.h"

#include "Algo/Import/import.h"
#include "Algo/Geometry/boundingbox.h"
#include "Algo/Render/vbo_MapRender.h"


using namespace CGoGN ;


/**
 * Struct that contains some informations about the types of the manipulated objects
 * Mainly here to be used by the algorithms that are parameterized by it
 */
46
struct PFP: public PFP_STANDARD
Pierre Kraemer's avatar
Pierre Kraemer committed
47
{
Pierre Kraemer's avatar
Pierre Kraemer committed
48
	// definition of the map
49
	typedef EmbeddedMap2 MAP;
Pierre Kraemer's avatar
Pierre Kraemer committed
50 51 52 53 54 55 56 57 58 59 60 61 62
};

/**
 * A class for a little interface and rendering
 */
class MyGlutWin: public Utils::SimpleGlutWin
{
public:
     void myRedraw();

     PFP::REAL gWidthObj;
     PFP::VEC3 gPosObj;

Sylvain Thery's avatar
Sylvain Thery committed
63
     Algo::Render::GL2::MapRender_VBO* m_render;
Pierre Kraemer's avatar
Pierre Kraemer committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

 	MyGlutWin(int* argc, char **argv, int winX, int winY) : SimpleGlutWin(argc, argv, winX, winY) {}
 	~MyGlutWin()
 	{
 		delete m_render ;
 	}
};

// Routine d'affichage
void MyGlutWin::myRedraw()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPushMatrix();

	// center the object
	float sc = 50.0f / gWidthObj;
	glScalef(sc, sc, sc);
	glTranslatef(-gPosObj[0], -gPosObj[1], -gPosObj[2]);

	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	glDisable(GL_LIGHTING);

	// draw the lines
	glColor3f(1.0f, 1.0f, 0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
88
	m_render->draw(Algo::Render::GL2::LINES);
Pierre Kraemer's avatar
Pierre Kraemer committed
89 90 91 92 93

	// draw the faces
	glEnable(GL_POLYGON_OFFSET_FILL);
	glPolygonOffset(1.0f, 1.0f);
	glColor3f(0.0f, 0.5f, 0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
94
	m_render->draw(Algo::Render::GL2::TRIANGLES);
Pierre Kraemer's avatar
Pierre Kraemer committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108
	glDisable(GL_POLYGON_OFFSET_FILL);

	glPopMatrix();
}



template<typename PFP>
void fonction_exemple(typename PFP::MAP& map, const AttributeHandler<typename PFP::VEC3>& position, Dart d)
{
	// affiche les Sommets d'une face:
	Dart e=d;
	do
	{
109
		CGoGNout << "Position brin"<<e.index<< " = "<< position[e]<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
110 111 112 113 114 115 116 117 118 119 120 121
		e = map.phi1(e); // dans l'autre sens myMap.phi_1(e)
	}while (e!=d);
}




int main(int argc, char **argv)
{
	// declaration of the map
	PFP::MAP myMap;

Pierre Kraemer's avatar
Pierre Kraemer committed
122 123 124
	std::vector<std::string> attrNames ;
	if(!Algo::Import::importMesh<PFP>(myMap, argv[1], attrNames))
	{
125
		CGoGNerr << "could not import " << argv[1] << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
126 127 128
		return 1 ;
	}
	// cree un handler d'attribut pour la position des points (créé lors de l'import)
129
	AttributeHandler<PFP::VEC3> position = myMap.getAttribute<PFP::VEC3>(VERTEX, attrNames[0]) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
130
	// cree un handler pour les normales aux sommets
131
	AttributeHandler<PFP::VEC3> normal = myMap.addAttribute<PFP::VEC3>(VERTEX, "normal");
Pierre Kraemer's avatar
Pierre Kraemer committed
132 133 134


//	// parcours de tous les brins de la carte:
Pierre Kraemer's avatar
Pierre Kraemer committed
135 136
//	for (Dart d = myMap.begin(); d != myMap.end(); myMap.next(d))
//		fonction_exemple<PFP>(myMap, position, d);
Pierre Kraemer's avatar
Pierre Kraemer committed
137 138 139

	// parcours de toutes les faces de la carte:
	DartMarker mf(myMap);
Pierre Kraemer's avatar
Pierre Kraemer committed
140
	for (Dart d = myMap.begin(); d != myMap.end(); myMap.next(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
141 142 143
	{
		if (! mf.isMarked(d)) // si d non marque:
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
144
			fonction_exemple<PFP>(myMap, position, d);
Pierre Kraemer's avatar
Pierre Kraemer committed
145
			// marque tous les brins de la face de d
146
			mf.markOrbit(FACE, d);
Pierre Kraemer's avatar
Pierre Kraemer committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
		}
	}

	// pas obligatoire (fait dans le destructeur)
	mf.unmarkAll();


    // instanciation of the interface
	MyGlutWin mgw(&argc, argv, 800, 800);

    // computation of the bounding box
    Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position);
    mgw.gWidthObj = std::max<PFP::REAL>(std::max<PFP::REAL>(bb.size(0), bb.size(1)), bb.size(2));
    mgw.gPosObj = (bb.min() +  bb.max()) / PFP::REAL(2);

    // instanciation of the renderer (here using VBOs)
Sylvain Thery's avatar
Sylvain Thery committed
163
    mgw.m_render = new Algo::Render::GL2::MapRender_VBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
164 165

    // update the renderer (primitives and geometry)
Sylvain Thery's avatar
Sylvain Thery committed
166 167 168
    mgw.m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::TRIANGLES);
    mgw.m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::LINES);
    mgw.m_render->updateData(Algo::Render::GL2::POSITIONS, position);
Pierre Kraemer's avatar
Pierre Kraemer committed
169 170 171 172 173

    mgw.mainLoop();

    return 0;
}