1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
// Software Guide : BeginLatex
//
// \index{itk::SpatialObjectTransform}
//
// This example describes the different
// transformations and the Object and World "spaces" associated with a spatial
// object.
//
// \begin{description}
// \item[Object Space]. SpatialObjects have one primary coordinate space
// that is readily available
// to them, their \code{ObjectSpace}. This is the space in which the object
// was inherently defined. No transforms are applied to the points/values that
// get/set into this space. All children of an object are added into this
// space.
//
// \item[ObjectToParentTransform]. SpatialObjects have only one transform
// that they directly control, their
// \code{ObjectToParentTransform}. This transform specifies how an object's
// \code{ObjectSpace} is
// transformed to fit into its parent's \code{ObjectSpace}. The
// \code{ObjectToParentTransform} is an affine transform, and it is confirmed
// to be invertible when assigned, or the assignment fails.
//
// \item[WorldSpace]. \code{WorldSpace} is not directly controlled by any
// SpatialObject except the
// SpatialObject at the top level of the parent-child tree hierarchy of
// Spatial Objects. That is, any SpatialObject that does not have a parent
// exists in a \code{WorldSpace} that is defined by applying its
// \code{ObjectToParentTransform} to its \code{ObjectSpace}.
//
// \end{description}
//
// Several member functions and variables are available to every SpatialObject
// so that they can readily access the WorldSpace in which they exist:
//
// \begin{description}
//
// \item[ProtectedComputeObjectToWorldTransform()]: This function is called
// whenever \code{Update()} is called. It composes the object's
// \code{ObjectToParentTransform}
// with its parent's cached \code{ObjectToWorldTransform}, to determine the
// transform from the object's \code{ObjectSpace} to \code{WorldSpace}.
// This transform is
// always invertible. This call will cause all children objects to also
// update their cached \code{ObjectToWorldTransform}. This function should
// be called on the top level object (via \code{Update()}) once all children
// object's
// \code{ObjectToParentTransform}s have been set. This function should
// be called
// on children objects when their \code{ObjectToParentTransform}s have been
// changed.
//
// \item[GetObjectToWorldTransform()]: Returns the cached
// \code{ObjectToWorldTransform}.
// It is the user's responsibility to call \code{Update()} (and thereby
// \code{ProtectedComputeObjectToWorldTransform()}) when
// necessary, prior to calling \code{GetObjectToWorldTransform()}, otherwise
// the returned transform may be "stale."
//
// \item[SetObjectToWorldTransform()]: This function updates the object's
// \code{ObjectToParentTransform}, using an inverse of the parent's cached
// \code{ObjectToWorldTransform}, so that the composition of those transforms
// equal the transform passed to this function. If an object has no parent,
// its \code{ObjectToParentTransform} is equal to its
// \code{ObjectToWorldTransform}.
//
// \end{description}
//
// Software Guide : EndLatex
#include "itkSpatialObject.h"
int
main(int, char *[])
{
// Software Guide : BeginLatex
//
// Like the first example, we create two spatial objects and give them the
// names \code{First Object} and \code{Second Object}, respectively.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
using SpatialObjectType = itk::SpatialObject<2>;
using TransformType = SpatialObjectType::TransformType;
auto object1 = SpatialObjectType::New();
object1->GetProperty().SetName("First Object");
auto object2 = SpatialObjectType::New();
object2->GetProperty().SetName("Second Object");
object1->AddChild(object2);
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// First we define a scaling factor of 2 for the object2.
// This is done by setting the Scale of the \code{ObjectToParentTransform}.
//
// Note that this scaling would also apply to the children of object2,
// if it had children. If you wish to scale an object, but not its
// children, then those children aren't actually ``children'', but they are
// siblings. So, you should insert a \code{GroupSpatialObject} that holds
// both the object and its siblings as children. Then you can manipulate
// the object's transform/scaling independent of its siblings in that group,
// and if you wish to transform the object and its siblings, you apply that
// transform to the group.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
double scale[2];
scale[0] = 2;
scale[1] = 2;
object2->GetModifiableObjectToParentTransform()->Scale(scale);
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Next, we apply an offset on the \code{ObjectToParentTransform} to
// \code{object1}
// which will also cause a translation of its child, \code{object2}.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
TransformType::OffsetType object1Offset;
object1Offset[0] = 4;
object1Offset[1] = 3;
object1->GetModifiableObjectToParentTransform()->SetOffset(object1Offset);
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// To realize the previous operations on the transformations, we should
// invoke the \code{Update()} that recomputes all dependent transformations.
//
// By calling this function on \code{object1}, it will also descend to its
// children, thereby also updating the \code{ObjectToWorldTransform} for
// \code{object2}.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
object1->Update();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We can now display the \code{ObjectToWorldTransform} for both objects.
// One should notice that the only valid members of the Affine
// transformation are a Matrix and an Offset. For instance, when we invoke
// the \code{Scale()} method the internal Matrix is recomputed to reflect
// this change.
//
// The AffineTransform performs the following
// computation
//
// \begin{equation}
// X' = R \cdot \left( S \cdot X - C \right) + C + V
// \end{equation}
//
// Where $R$ is the rotation matrix, $S$ is a scaling factor, $C$ is the
// center of rotation and $V$ is a translation vector or offset. Therefore
// the affine matrix $M$ and the affine offset $T$ are defined as:
//
// \begin{equation}
// M = R \cdot S
// \end{equation}
// \begin{equation}
// T = C + V - R \cdot C
// \end{equation}
//
// This means that \code{Scale()} and \code{GetOffset()}
// as well as the \code{GetMatrix()} might not be set to the
// expected value, especially if the transformation results from a
// composition with another transformation since the composition is done
// using the Matrix and the Offset of the affine transformation.
//
// Next, we show the two affine transformations corresponding to the two
// objects.
//
// First, the \code{ObjectToParentTransform} for \code{object2}:
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
std::cout << "object2 ObjectToParent Matrix: " << std::endl;
std::cout << object2->GetObjectToParentTransform()->GetMatrix()
<< std::endl;
std::cout << "object2 ObjectToParent Offset: ";
std::cout << object2->GetObjectToParentTransform()->GetOffset()
<< std::endl;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Second, the \code{ObjectToWorldTransform} that is derived
// from the parent-child hierarchy and the composition of the corresponding
// \code{ObjectToParentTransform}s, computed by called to
// \code{Update()}, and cached for efficient subsequent use, for
// \code{object2}:
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
std::cout << "object2 ObjectToWorld Matrix: " << std::endl;
std::cout << object2->GetObjectToWorldTransform()->GetMatrix() << std::endl;
std::cout << "object2 ObjectToWorld Offset: ";
std::cout << object2->GetObjectToWorldTransform()->GetOffset() << std::endl;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We can also update an object's \code{ObjectToParentTransform} by
// changing its \code{ObjectToWorldTransform} and then calling
// \code{ComputeObjectToParentTransform()},
// which changes the \code{ObjectToParentTransform} so as to achieve the
// cached \code{ObjectToWorldTransform}.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
TransformType::OffsetType Object1ToWorldOffset;
Object1ToWorldOffset[0] = 3;
Object1ToWorldOffset[1] = 3;
object1->GetModifiableObjectToWorldTransform()->SetOffset(
Object1ToWorldOffset);
object1->ComputeObjectToParentTransform();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Finally, we display the resulting affine transformations. First,
// for the \code{ObjectToParentTransform} for \code{object1}.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
std::cout << "object1 ObjectToParent Matrix: " << std::endl;
std::cout << object1->GetObjectToParentTransform()->GetMatrix()
<< std::endl;
std::cout << "object1 ObjectToParent Offset: ";
std::cout << object1->GetObjectToParentTransform()->GetOffset()
<< std::endl;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Second, for the \code{ObjectToWorldTransform} for \code{object2}.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
std::cout << "object2 ObjectToWorld Matrix: " << std::endl;
std::cout << object2->GetObjectToWorldTransform()->GetMatrix() << std::endl;
std::cout << "object2 ObjectToWorld Offset: ";
std::cout << object2->GetObjectToWorldTransform()->GetOffset() << std::endl;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Also, as a child is disconnected from its parent, it should not move;
// so its \code{ObjectToParentTransform} should be updated to match its
// \code{ObjectToWorldTransform}.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
object1->RemoveChild(object2);
object2->Update();
std::cout << "object2 ObjectToWorld Matrix: " << std::endl;
std::cout << object2->GetObjectToWorldTransform()->GetMatrix() << std::endl;
std::cout << "object2 ObjectToWorld Offset: ";
std::cout << object2->GetObjectToWorldTransform()->GetOffset() << std::endl;
std::cout << "object2 ObjectToParent Matrix: " << std::endl;
std::cout << object2->GetObjectToParentTransform()->GetMatrix()
<< std::endl;
std::cout << "object2 ObjectToParent Offset: ";
std::cout << object2->GetObjectToParentTransform()->GetOffset()
<< std::endl;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The output of this second example looks like the following:
// \small
// \begin{verbatim}
// object2 ObjectToParent Matrix:
// 2 0
// 0 2
// object2 ObjectToParent Offset: 0 0
// object2 ObjectToWorld Matrix:
// 2 0
// 0 2
// object2 ObjectToWorld Offset: 4 3
//
// object1 ObjectToParent Matrix:
// 1 0
// 0 1
// object1 ObjectToParent Offset: 3 3
// object2 ObjectToWorld Matrix:
// 2 0
// 0 2
// object2 ObjectToWorld Offset: 7 6
//
// object2 ObjectToParent Matrix:
// 2 0
// 0 2
// object2 ObjectToParent Offset: 7 6
// object2 ObjectToWorld Matrix:
// 2 0
// 0 2
// object2 ObjectToWorld Offset: 7 6
// \end{verbatim}
// \normalsize
//
// Software Guide : EndLatex
return EXIT_FAILURE;
}
|