gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
affineTransformation.cpp
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 //
6 // Contributed by K. Hillewaert
7 
8 #include <cmath>
9 #include <vector>
10 
11 #include "fullMatrix.h"
12 
13 template <class FLOAT>
14 bool computeAffineTransformation(const FLOAT *rc, const FLOAT *ra,
15  const FLOAT *tr, std::vector<double> &tfo)
16 {
17  // rotation matrix
18  double rotData[9] = {1., 0., 0., 0., 1., 0., 0., 0., 1.};
19  fullMatrix<double> rot(rotData, 3, 3);
20 
21  // rotation about x-axis
22  if(ra[0] != 0.) {
23  fullMatrix<double> tmp(rot);
24  const double ca = std::cos(ra[0]), sa = std::sin(ra[0]);
25  double rotX[9] = {1., 0., 0., 0., ca, -sa, 0., sa, ca};
26  rot.gemm(fullMatrix<double>(rotX, 3, 3), tmp, 1., 0.);
27  }
28 
29  // rotation about y-axis
30  if(ra[1] != 0.) {
31  fullMatrix<double> tmp(rot);
32  const double ca = std::cos(ra[1]), sa = std::sin(ra[1]);
33  double rotY[9] = {ca, 0., sa, 0., 1., 0., -sa, 0., ca};
34  rot.gemm(fullMatrix<double>(rotY, 3, 3), tmp, 1., 0.);
35  }
36 
37  // rotation about z-axis
38  if(ra[2] != 0.) {
39  fullMatrix<double> tmp(rot);
40  const double ca = std::cos(ra[2]), sa = std::sin(ra[2]);
41  double rotZ[9] = {ca, -sa, 0., sa, ca, 0., 0, 0, 1.};
42  rot.gemm(fullMatrix<double>(rotZ, 3, 3), tmp, 1., 0.);
43  }
44 
45  // compute displacement from rotation center (I-R)*x and translation t
46  double rotCenterData[3] = {rc[0], rc[1], rc[2]};
47  fullVector<double> rotCenter(rotCenterData, 3);
48  fullVector<double> dispRot(3);
49  rot.mult(rotCenter, dispRot);
50  double disp[3];
51  for(int i = 0; i < 3; i++) disp[i] = tr[i] + rc[i] - dispRot(i);
52 
53  // copy to tfo
54  tfo.clear();
55  tfo.reserve(16.);
56  for(int i = 0; i < 3; i++) {
57  for(int j = 0; j < 3; j++) tfo.push_back(rot(i, j));
58  tfo.push_back(disp[i]);
59  }
60  for(int i = 0; i < 3; i++) tfo.push_back(0.);
61  tfo.push_back(1.);
62 
63  return true;
64 }
65 
66 // explicit instantiations for float and double
67 template bool computeAffineTransformation(const float *rc, const float *ra,
68  const float *tr,
69  std::vector<double> &tfo);
70 template bool computeAffineTransformation(const double *rc, const double *ra,
71  const double *tr,
72  std::vector<double> &tfo);
73 
74 template <class FLOAT>
75 bool getAffineTransformationParameters(const std::vector<double> &tfo,
76  FLOAT *rc, FLOAT *ra, FLOAT *tr)
77 {
78  // rotation angles
79  ra[0] = std::atan2(-tfo[2 * 4 + 1], tfo[2 * 4 + 2]);
80  ra[1] = std::asin(tfo[2 * 4 + 0]);
81  ra[2] = std::atan2(-tfo[1 * 4 + 0], tfo[0 * 4 + 0]);
82 
83  // translation
84  for(int i = 0; i < 3; i++) rc[i] = 0.;
85  for(int i = 0; i < 3; i++) tr[i] = tfo[4 * i + 3];
86 
87  return true;
88 }
89 
90 // explicit instantiations for float and double
91 template bool getAffineTransformationParameters(const std::vector<double> &tfo,
92  float *rc, float *ra,
93  float *tr);
94 template bool getAffineTransformationParameters(const std::vector<double> &tfo,
95  double *rc, double *ra,
96  double *tr);
97 
98 bool invertAffineTransformation(const std::vector<double> &tfo,
99  std::vector<double> &newTfo)
100 {
101  fullMatrix<double> inv(4, 4);
102  for(int i = 0; i < 4; i++)
103  for(int j = 0; j < 4; j++) inv(i, j) = tfo[i * 4 + j];
104  inv.invertInPlace();
105  newTfo.clear();
106  for(int i = 0; i < 4; i++)
107  for(int j = 0; j < 4; j++) newTfo.push_back(inv(i, j));
108  return true;
109 }
110 
111 bool setUnitAffineTransformation(std::vector<double> &tfo)
112 {
113  tfo.resize(16, 0);
114  for(int i = 0; i < 16; i += 5) tfo[i] = 1;
115  return true;
116 }
fullVector< double >
invertAffineTransformation
bool invertAffineTransformation(const std::vector< double > &tfo, std::vector< double > &newTfo)
Definition: affineTransformation.cpp:98
fullMatrix< double >
fullMatrix::invertInPlace
bool invertInPlace()
Definition: fullMatrix.h:662
computeAffineTransformation
bool computeAffineTransformation(const FLOAT *rc, const FLOAT *ra, const FLOAT *tr, std::vector< double > &tfo)
Definition: affineTransformation.cpp:14
getAffineTransformationParameters
bool getAffineTransformationParameters(const std::vector< double > &tfo, FLOAT *rc, FLOAT *ra, FLOAT *tr)
Definition: affineTransformation.cpp:75
fullMatrix::gemm
void gemm(const fullMatrix< scalar > &a, const fullMatrix< scalar > &b, scalar alpha=1., scalar beta=1., bool transposeA=false, bool transposeB=false)
Definition: fullMatrix.h:580
fullMatrix::mult
void mult(const fullVector< scalar > &x, fullVector< scalar > &y) const
Definition: fullMatrix.h:487
setUnitAffineTransformation
bool setUnitAffineTransformation(std::vector< double > &tfo)
Definition: affineTransformation.cpp:111
fullMatrix.h