1 /****************************************************************************
2 * MeshLab                                                           o o     *
3 * An extendible mesh processor                                    o     o   *
4 *                                                                _   O  _   *
5 * Copyright(C) 2005, 2009                                          \/)\/    *
6 * Visual Computing Lab                                            /\/|      *
7 * ISTI - Italian National Research Council                           |      *
8 *                                                                    \      *
9 * All rights reserved.                                                      *
10 *                                                                           *
11 * This program is free software; you can redistribute it and/or modify      *
12 * it under the terms of the GNU General Public License as published by      *
13 * the Free Software Foundation; either version 2 of the License, or         *
14 * (at your option) any later version.                                       *
15 *                                                                           *
16 * This program is distributed in the hope that it will be useful,           *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
20 * for more details.                                                         *
21 *                                                                           *
22 ****************************************************************************/
23 
24 #ifndef PRIMITIVE_H
25 #define PRIMITIVE_H
26 
27 #include <QThread>
28 #include <vcg/math/matrix44.h>
29 #include <vcg/complex/algorithms/closest.h>
30 #include <vcg/space/index/grid_static_ptr.h>
31 #include <vcg/space/point2.h>
32 #include <vcg/space/point3.h>
33 //#include <../code/lib/non_linear_minimization/non_linear_minimization.h>
34 //#include <wrap/minpack/minpack.h>
35 #include <vcg/math/point_matching.h>
36 #include <vcg/math/histogram.h>
37 #include "../../meshlab/meshmodel.h"
38 
39 
40 
41 
42 typedef vcg::GridStaticPtr<CMeshO::VertexType> GridType;
43 
44 int callEvaluateError(void *data,int m_dat,int n,const double* par,double* fvec,int iflag);
45 
46 class Primitive : public QThread{
47 
48 	Q_OBJECT
49 
50 protected:
51 	double												*parameters;					//Lista parametri delle primitiva.
52 	int														numParameters;				//Numero parametri primitiva.
53 	vcg::Matrix44f								orientation;					//Posizione ed orientamento della primitiva, la ricava il costruttore dal gesto2d e gesto3d e viene modificata dalla RigidTranformation.
54 	CMeshO												*TotMesh;							//La Mesh intera serve per la GetClosestVertex.
55 	CMeshO												*selectionMesh;				//La sotto Mesh
56 	std::vector<vcg::Point2i>			*gesture2D;						//Il gesto2D fatto dall'utente sulla primiva.
57 	std::vector<vcg::Point3f>			*gesture3D;						//La corrispondente proezione del gesto2d sulla mesh.
58 	std::vector<vcg::Point3f>			cloudNearPoints;			//Sono tutti i punti della Mesh pi vicini a quelli del Sampling.
59 	std::vector<vcg::Point3f>			cloudSampling;				//Il risultato del Sampling.
60 	GridType											*grid;							  //La sotto mesh nella struttura di ricerca griglia statica.
61 	double												totDistance;
62 	double												lastTotDistance;
63 	std::vector<double>						weight;
64 	std::vector<double>						myfvec;
65 
66 	//Begin_Debug:
67 		std::vector<std::vector<vcg::Point3f> >			DebugCloudMesh;
68 		std::vector<std::vector<vcg::Point3f> >			DebugCloudNearPoints;
69 		std::vector<std::vector<vcg::Point3f> >			DebugCloudSampling;
70 		std::vector<vcg::Matrix44f>									DebugOrientationMatrix;
71 	//End_Debug:
72 
73 	bool showCloudNearPoints;
74 	bool showLines;
75 	bool showSelectionMesh;
76 	bool showCloudSampling;
77 	bool showInObjectSpace;
78 	bool showStepMode;
79 	int  indexOfstep;
80 
81 
82 
83 public:
84 	Primitive(std::vector<vcg::Point2i> *_gesture2D,std::vector<vcg::Point3f>	*_gesture3D,CMeshO *_TotMesh);
85 	virtual void Sampling(const double *par)=0;
86 	virtual void InitSampling(double *par)=0;
87 	virtual void Draw()=0;
88 	//virtual void EvaluateError(double* par,int m_dat,double* fvec,void *data,NLMin<Primitive>::lm_status_type *status);
89 	virtual int EvaluateError(void *data,int m_dat,int n,const double* par,double* fvec,int iflag);
90 	virtual void run();
91 	void DrawDebug();
92 	void DrawDebugInWorldSpace();
93 	void DrawDebugInObjectSpace();
94 	void DrawDebugInStapMode();
95 	virtual ~Primitive();
96 
97 public slots:
98 	void change_checkBoxCloudNearPoints(bool);
99 	void change_checkBoxLines(bool);
100 	void change_checkBoxSelectionMesh(bool);
101 	void change_checkBoxCloudSampling(bool);
102 	void change_checkBoxObjectSpace(bool);
103 	void change_checkBoxStepMode(bool);
104 	//spinBox
105 	void change_spinBoxStepMode(int);
106 
107 signals:
108 	void updateGlArea();
109 	void updateDebugStapModeSize(int);
110 
111 private:
112 	void RigidTranformation();
113 	void NonLinearMinimization();
114 	void UpdateMesh_and_Grid_and_Near(const vcg::Matrix44f &m);
115 	void UpdateWeight();
116 	void InitDebug();
117 };
118 #endif