]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsKernelEditorGraphic/GConnectorModel.cxx
v1.1.0 BUG 1403
[bbtkGEditor.git] / lib / EditorGraphicBBS / bbsKernelEditorGraphic / GConnectorModel.cxx
1 /*=========================================================================                                                                               
2 Program:   bbtk
3 Module:    $RCSfile$
4 Language:  C++
5 Date:      $Date$
6 Version:   $Revision$
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30
31 /**
32 *  \file 
33 *  \brief Class bbtk::GConnectorModel 
34 */
35
36
37 #include "GConnectorModel.h"
38
39 namespace bbtk
40 {
41
42         //=========================================================================
43
44         GConnectorModel::GConnectorModel()
45         {               
46                 _startPort = NULL;
47                 _endPort = NULL;
48         }
49
50         //=========================================================================
51
52         GConnectorModel::~GConnectorModel()
53         {
54         }
55
56         //=========================================================================
57
58         void GConnectorModel::setStartPort(GPortModel* startPort)
59         {
60                 _startPort = startPort;
61                 startPort->setConnected(true);
62                 
63                 double xCenter,yCenter,zCenter;
64                 startPort->getCenter(xCenter,yCenter,zCenter);
65                 double xFport,yFport,zFport;
66                 startPort->getFinalPoint(xFport,yFport,zFport);
67
68                 setInicPoint(xCenter,yFport,zCenter);
69         }
70
71         //=========================================================================
72         
73         void GConnectorModel::setEndPort(GPortModel* endPort)
74         {
75                 _endPort = endPort;
76                 endPort->setConnected(true);
77
78                 double xCenter,yCenter,zCenter;         
79                 endPort->getCenter(xCenter,yCenter,zCenter);
80                 double xIport,yIport,zIport;
81                 endPort->getInicPoint(xIport,yIport,zIport);
82
83                 setFinalPoint(xCenter,yIport,zCenter);
84
85                 // Sets the last point of the contour model in the position of the end input port
86                 manualPoint* point = _model->GetManualPoint(_model->GetSizeLstPoints()-1);
87                 point->SetPoint(xCenter,yIport,zCenter);
88         }
89
90         //=========================================================================
91         
92         GPortModel* GConnectorModel::getStartPort()
93         {
94                 return _startPort;
95         }
96
97         //=========================================================================
98         
99         GPortModel* GConnectorModel::getEndPort()
100         {
101                 return _endPort;
102         }
103         
104         //=========================================================================
105
106         manualContourModel* GConnectorModel::getManualContourModel()
107         {
108                 return _model;
109         }
110
111         //=========================================================================
112         
113         void GConnectorModel::setManualContourModel(manualContourModel* model)
114         {
115                 _model = model;
116         }
117
118         //=========================================================================
119
120         void GConnectorModel::updateStartEndPoints()
121         {
122                 
123                 //back point
124                 double bsx,bsy; // back start for x and y
125                 double nsx,nsy;
126                 double bex,bey; // new start for x and y
127                 double nex,ney;
128                 
129                 //Start Point
130                 double xCenter,yCenter,zCenter;         
131                 _startPort->getCenter(xCenter,yCenter,zCenter);
132                 double xport,yport,zport;
133                 _startPort->getFinalPoint(xport,yport,zport);
134
135                 // Refresh the position of the contourModel first point with the data of the connector model
136                 manualPoint* point = _model->GetManualPoint(0);
137                 bsx=point->GetX(); 
138                 bsy=point->GetY(); 
139                 point->SetPoint(xCenter,yport,zCenter);
140                 nsx=xCenter;
141                 nsy=yport;
142                 
143                 //End Point     
144                 _endPort->getCenter(xCenter,yCenter,zCenter);
145                 _endPort->getInicPoint(xport,yport,zport);
146
147                 // Refresh the position of the contourModel last point with the data of the connector model
148                 point = _model->GetManualPoint(_model->GetSizeLstPoints()-1);
149                 bex=point->GetX(); 
150                 bey=point->GetY(); 
151                 point->SetPoint(xCenter,yport,zCenter);
152                 nex=xCenter;
153                 ney=yport;
154
155                 
156                 // moving point of the spline connection
157                 if ( ((bsx!=nsx)&&(bex!=nex)) ||  ((bsy!=nsy)&&(bey!=ney)) )
158                 {
159                         double difx=bsx-nsx; 
160                         double dify=bey-ney;
161                         double tmpX, tmpY, tmpZ;
162                         int i,size=_model->GetSizeLstPoints();
163                         if (size>2)
164                         {
165                                 for (i=1;i<size-1;i++)
166                                 {
167                                         point = _model->GetManualPoint(i);
168                                         tmpX = point->GetX();
169                                         tmpY = point->GetY();
170                                         tmpZ = point->GetZ();
171                                         point->SetPoint(tmpX-difx,tmpY-dify,tmpZ);
172                                 } // for i
173                         }// if size
174                 } // if bsx nsx  bex nex   bsy nsy bey ney 
175         }
176
177         //=========================================================================
178
179         void GConnectorModel::disconnectConnection()
180         {
181                 if(_startPort!=NULL)
182                 {
183                         _startPort->setConnected(false);                        
184                 }
185
186                 if(_endPort!=NULL)
187                 {
188                         _endPort->setConnected(false);
189                 }
190         }
191
192         //=========================================================================
193
194         void GConnectorModel::save(std::string &content)
195         {
196                 char buffer[50];
197
198                 content+="CONNECTION\n";
199
200                 // Connection info
201                 content+=_startPort->getParentBox()->getBBTKName();
202                 content+=":";
203                 content+=_startPort->getBBTKName();
204                 content+=":";
205                 content+=_endPort->getParentBox()->getBBTKName();
206                 content+=":";
207                 content+=_endPort->getBBTKName();
208                 content+="\n";
209                 
210                 int i,sizeLstPoints = _model->GetSizeLstPoints()-2;
211                 content+="NumberOfControlPoints:";
212                 sprintf (buffer, "%d", sizeLstPoints );
213                 content+=buffer;
214                 content+="\n";
215                 
216                 for (i=0;i<sizeLstPoints;i++)
217                 {
218                         manualPoint *mp = _model->GetManualPoint(i+1);
219                         sprintf (buffer, "%f:%f:%f\n", (float)mp->GetX(),(float)mp->GetY(),(float)mp->GetZ() );
220                         content+=buffer;
221                 }               
222         }
223
224         //=========================================================================
225
226 }  // EO namespace bbtk
227
228 // EOF
229