I am trying to create a rotation matrix using Eigen::Matrix3d, but can't seem to do it accurately..
void rot(double x,double y,double z)
{
Eigen::Matrix3d x_r ((double)1,(double)0,(double)0,
(double)0,cos(x),-sin(x),
(double)0,sin(x),cos(x));
Eigen::Matrix3d y_r (cos(y),(double)0,sin(y),
(double)0,(double)1,(double)0,
-sin(y),(double)0,cos(y));
Eigen::Matrix3d z_r (cos(z),-sin(z),(double)0,
sin(z),cos(z),(double)0,
(double)0,(double)0,(double)1) ;
Eigen::Matrix3d rot = z_r*y_r*x_r;
}
I can't crate x_r,y_r,z_r for some weird reason?.. Could someone elaborate what i am doing wrong?
Error message i am receiving is:
error: no matching function for call to 'Eigen::Matrix<double, 3, 3>::Matrix(double, double, double, double, double, double, double, double, double)'
First i thought i was because i was using a combination of double and intwhich is why i typecasted it in the first place..
Try with
Eigen::Matrix3d x_r;
Eigen::Matrix3d y_r;
Eigen::Matrix3d z_r;
x_r << 1.0, 0.0, 0.0,
0.0, cos(x), -sin(x),
0.0 ,sin(x), cos(x);
y_r << cos(y), 0.0, sin(y),
0.0, 1.0, 0.0,
-sin(y), 0.0, cos(y);
z_r << cos(z), -sin(z), 0.0,
sin(z), cos(z), 0.0,
0.0, 0.0, 1.0;
Eigen::Matrix3d rot = z_r*y_r*x_r;
Seems that there isn't the constructor of Eigen::Matrix3d you're trying to use: a constructor that initializes the values directly.
In your example you try to construct the matrix by passing the matrix entries in the constructor. But the constructor does not take the matrix entries as arguments, even if it is a fixed (matrix) size type like Eigen::Matrix3d. You can set the matrix entries with the overloaded operator() after the matrix object is constructed, e.g.:
Eigen::Matrix3d M;
M( 0,0 ) = 1.0;
M( 0,1 ) = 0.0;
M( 0,2 ) = 0.0;
M( 1,0 ) = 0.0;
M( 1,1 ) = cos( x );
M( 1,2 ) = -sin( x );
M( 1,0 ) = 0.0;
M( 1,1 ) = sin( x );
M( 1,2 ) = cos( x );
Related
I am trying to use plane to plane transformation method in glm.
But rotations are wrong, I use this code in Eigen, also in CGAL libraries. Everything was working correctly. But something off is in GLM.
Can someone tell me why this orientation code produces wrong results?
inline glm::mat4 plane_to_plane(
const std::array<glm::vec3, 4>& plane_0,
const std::array<glm::vec3, 4>& plane_1
//IK::Vector_3 O1, IK::Vector_3 X1, IK::Vector_3 Y1, IK::Vector_3 Z1
) {
// transformation maps P0 to P1, P0+X0 to P1+X1, ...
//Move to origin -> T0 translates point P0 to (0,0,0)
glm::mat4 T0 = glm::translate(glm::vec3(0 - plane_0[0].x, 0 - plane_0[0].y, 0 - plane_0[0].z));
//Rotate ->
glm::mat3 F0(
plane_0[1].x, plane_0[1].y, plane_0[1].z,
plane_0[2].x, plane_0[2].y, plane_0[2].z,
plane_0[3].x, plane_0[3].y, plane_0[3].z
);
glm::mat3 F1(
plane_1[1].x, plane_1[2].x, plane_1[3].x,
plane_1[1].y, plane_1[2].y, plane_1[3].y,
plane_1[1].z, plane_1[2].z, plane_1[3].z
);
glm::mat3 R = F1 * F0;
glm::mat4 R_ = glm::mat4(R);
//Move to 3d -> T1 translates (0,0,0) to point P1
glm::mat4 T1 = glm::translate(glm::vec3(plane_1[0].x - 0, plane_1[0].y - 0, plane_1[0].z - 0));
return T1 * R_ * T0;
}
I use two planes for the transformation:
std::vector<float> plines_v = {
-0.5, -0.5, 0, 0.5, -0.5, 0, 0.5, 0, 0, 0.307011, 0.5, 0, 0.307011, 0.5, 0.136471, 0.5, 0, 0.136471, 0.5, -0.5, 0.136471, -0.5, -0.5, 0.136471, -0.5, 0.5, 0.136471, -0.5, 0.5, 0, -0.5, -0.5, 0
};
auto plane_0 = std::array<glm::vec3, 4>{
glm::vec3(-0.5, -0.5, 0),
glm::vec3(1, 0, 0),
glm::vec3(0, 1, 0),
glm::vec3(0, 0, 1),
};
auto plane_1 = std::array<glm::vec3, 4>{
glm::vec3(-0.5, -0.5, 0),
glm::vec3(0.912851, 0.035424, -0.406752),
glm::vec3(0.225204, -0.874665, 0.429238),
glm::vec3(-0.340566, -0.483432, -0.806417),
};
auto xform = opengl_transform::plane_to_plane(plane_0, plane_1);
for (int i = 0; i < plines_v.size(); i += 3) {
glm::vec4 v(plines_v[i + 0], plines_v[i + 1], plines_v[i + 2], 1);
v = xform*v;
plines_v[i + 0] = v.x;
plines_v[i + 1] = v.y;
plines_v[i + 2] = v.z;
}
Long story short, I tried to inverse R matrix with the following code that seems to work.
Any ideas how to write F0 and F1 matrices without inverting them afterwards?
inline glm::mat4 plane_to_plane(
const std::array<glm::vec3, 4>& plane_0,
const std::array<glm::vec3, 4>& plane_1
//IK::Vector_3 O1, IK::Vector_3 X1, IK::Vector_3 Y1, IK::Vector_3 Z1
) {
// transformation maps P0 to P1, P0+X0 to P1+X1, ...
//Move to origin -> T0 translates point P0 to (0,0,0)
glm::mat4 T0 = glm::translate(glm::vec3(0 - plane_0[0].x, 0 - plane_0[0].y, 0 - plane_0[0].z));
//Rotate ->
glm::mat3 F0(
plane_0[1].x, plane_0[1].y, plane_0[1].z,
plane_0[2].x, plane_0[2].y, plane_0[2].z,
plane_0[3].x, plane_0[3].y, plane_0[3].z
);
glm::mat3 F1(
plane_1[1].x, plane_1[2].x, plane_1[3].x,
plane_1[1].y, plane_1[2].y, plane_1[3].y,
plane_1[1].z, plane_1[2].z, plane_1[3].z
);
glm::mat3 R = F1 * F0;
glm::mat3 R_inv(1.0);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
R_inv[j][i] = R[i][j];
}
}
glm::mat4 R_ = glm::mat4(R_inv);
//Move to 3d -> T1 translates (0,0,0) to point P1
glm::mat4 T1 = glm::translate(glm::vec3(plane_1[0].x, plane_1[0].y, plane_1[0].z));
return T1 * R_ * T0;
}
I'm not sure if I should ask this at StackOverflow or at CodeReview. But because I couldn't find a similair problem, I'm posting it here.
ATM I'm optimizing the code of a simple image manipulation application.
One of the targets is to give all the convolution effect classes their respective matrix as their private variable. Because const-correctness this wasn't done earlier, but the quickfix to solve the introduced problems with const-correctness wasn't sparely with memory and cpu-cycles.
So I decided to init the convolutionMatrix at class-initializiation-level with a std::vector<std::vector<double>> since creating dozens of constructors to make each initialisation with std::array<std::array<double>,\d+>,\d+> possible, is inefficient.
BlurFilter::BlurFilter() : ColorEffect(), convolutionMatrix(
std::vector<std::vector<double>>{
std::vector<double>{ 0.0, 0.0, 1.0, 0.0, 0.0 },
std::vector<double>{ 0.0, 1.0, 1.0, 1.0, 0.0 },
std::vector<double>{ 1.0, 1.0, 1.0, 1.0, 1.0 },
std::vector<double>{ 0.0, 1.0, 1.0, 1.0, 0.0 },
std::vector<double>{ 0.0, 0.0, 1.0, 0.0, 0.0 }
} )
{
}
However. the application breaks at runtime in the Matrix constructor with an std::out_of_range-exception with the what()-message:
what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)"
Aka multidimensionalVector.size() is somehow 0.
Matrix::Matrix( const std::vector<std::vector<double>>& multidimensionalVector )
{
this->xLength = multidimensionalVector.size();
this->yLength = ( *multidimensionalVector.begin() ).size();
this->values = multidimensionalVector;
}
Honestly I don't understand why the size of the multidimentsionalVector is zero at that moment at all since I'm passing an initialized vector of vectors which could be -as shown- copy-constructed (or move-constructed) over to the values-variable of the Matrix class. Changing multidimensionalVector copy-by-value don't make the difference.
Could someone explain where and/or what is going wrong here?
(PS: I'd prefer answers written in own words (aka in Plain English) instead of citing directly from the C++ standard documents because of the used vague and confusing academic/scientific language).
FWIW, you can simplify your code quite a bit. Here's an example that works:
#include <iostream>
#include <vector>
struct Matrix
{
Matrix(const std::vector<std::vector<double>>& multidimensionalVector);
size_t xLength;
size_t yLength;
std::vector<std::vector<double>> values;
};
Matrix::Matrix( const std::vector<std::vector<double>>& multidimensionalVector ) : xLength(0), yLength(0), values(multidimensionalVector)
{
this->xLength = values.size();
std::cout << "xLength: " << xLength << std::endl;
if ( xLength > 0 )
{
this->yLength = ( *values.begin() ).size();;
}
std::cout << "yLength: " << yLength << std::endl;
}
struct BlurFilter
{
BlurFilter();
Matrix convolutionMatrix;
};
BlurFilter::BlurFilter() : convolutionMatrix( { { 0.0, 0.0, 1.0, 0.0, 0.0 },
{ 0.0, 1.0, 1.0, 1.0, 0.0 },
{ 1.0, 1.0, 1.0, 1.0, 1.0 },
{ 0.0, 1.0, 1.0, 1.0, 0.0 },
{ 0.0, 0.0, 1.0, 0.0, 0.0 } } )
{
}
int main()
{
BlurFilter f;
}
Well, This is embarrassing of me.
It seems I've fixed my own problem unintentionally when I was stripping and optimizing my code for posting this question. I didn't thought about to try out the streamlined code before posting it.
Putting
this->values = multidimensionalVector;
did the job.
The original of the Matrix-constructor which seemed to raise the std::out_of_range-exception was this:
Matrix::Matrix( const std::vector<std::vector<double>>& multidimensionalVector )
{
this->xLength = multidimensionalVector.size();
this->yLength = ( *multidimensionalVector.begin() ).size();
for( int x = 0; x < this->xLength; x++ )
{
for( int y = 0; y < this->yLength; y++ )
{
this->set( x, y, multidimensionalVector.at( x ).at( y ) );
}
}
}
Within Matrix::set(int x, int y, double newValue ), the x and y parameters are always checked if they're in between -1 and this->xLength && between -1 and this->yLength.
But the x and y parameters are never checked if they're in bounds with the (then not initialized) this->values...
I have a sorted std::vector<std::vector<double>> of x,y,z values as shown below,
0.0, 0.0, 0.0
0.0, 0.0, 0.1
0.0, 0.0, 0.2
0.0, 0.1, 0.0
0.0, 0.2, 0.1
0.0, 0.2, 0.3
I want to find all z values of a specific x and y value,
ex - z values of 0.0, 0.0, should return
0.0, 0.0, 0.0
0.0, 0.0, 0.1
0.0, 0.0, 0.2
I tried using
struct MatchDouble
{
MatchDouble(double _x,double _y) : x(_x), y(_y) {}
bool operator()(std::vector<double> &n)
{
return fabs(x - n[0]) < FLT_EPSILON && fabs(y - n[1]) < FLT_EPSILON;
}
private:
double x, y ;
};
it = find_if(allpoints.begin(), allpoints.end(),MatchDouble(0.0,0.0));
But this only gives me the iterator to a single vector value. What method is best suited to achieve this?
Thank you.
std::vector<std::vector<double>> ret;
std::copy_if(allpoints.begin(), allpoints.end(), std::back_inserter(ret), MatchDouble(0.0,0.0));
return ret;
This creates a new vector ret of the same type as allpoints and copy only the points of interest using copy_if
As per my question on Math Stackexchange:
I am working on a project for my 3D Graphics class. The project is built with C++ and OpenGL / Glut. Basically, I create a horizontal rectangle window, subdivided into two squares. On the left, I have a two dimensional coordinate plane, which allows the users to point and click and define a profile 'curve'. I then need to wrap this curve around the Y-axis n number of times.
So, would anyone be able to guide me as to how I would use Trigonometry to calculate the X and Z values of the successive points? If for example, a user clicks and creates the point:
(1, 1, 0)
And their sweep resolution (n) is set to, say, 10, then I need to redraw that point every 36 (360/10) degrees around the Y-axis.
Am I correct in assuming that Trigonometry will help me here? If so, can someone please enlighten me a bit as to how to calculate the location of a translated point in 3D space? It's been a while since I took Trig, and I don't believe we ever left 2D space.
EDIT: I attempted to use:
x'=xcos(theta)-zsin(theta)
y'=y
z'=xsin(theta)+zcos(theta)
, as per my understanding of AMPerrine's answer, and I don't think it worked as I'd hoped:
// this is in a loop
// setup the new angle
double angle = i>0 ? (360/sweepResolutionMod)*i : 0;
angle = angle * (M_PI/180);
// for each point...
for( int i=0; i<clickedPoints.size(); i++ )
{
// initial point, normalized
GLfloat tempX = (clickedPoints[i].x-250)/250;
GLfloat tempY = (clickedPoints[i].y-250)/250;
GLfloat tempZ = 0.0;
// log the initial point
cout << "(" << tempX << ", " << tempY << ", 0.0) by " << angle << " radians = ";
// generate the new point
GLfloat newX = (tempX * cos(angle)) - (tempZ * sin(angle));
GLfloat newY = tempY;
GLfloat newZ = (tempX * sin(angle)) - (tempZ * cos(angle));
// log the new point
cout << "(" << newX << ", " << newY << ", " << newZ << ")\n";
// render the new point
glVertex3d(newX, newY, newZ);
}
This produces no screen output, but console output of:
(0.048, -0.296, 0.0) by 0 radians = (0.048, -0.296, 0)
(0.376, -0.508, 0.0) by 0 radians = (0.376, -0.508, 0)
(0.72, -0.204, 0.0) by 0 radians = (0.72, -0.204, 0)
(0.652, 0.176, 0.0) by 0 radians = (0.652, 0.176, 0)
(0.368, 0.504, 0.0) by 0 radians = (0.368, 0.504, 0)
(0.048, -0.296, 0.0) by 0.628319 radians = (0.0388328, -0.296, 0.0282137)
(0.376, -0.508, 0.0) by 0.628319 radians = (0.30419, -0.508, 0.221007)
(0.72, -0.204, 0.0) by 0.628319 radians = (0.582492, -0.204, 0.423205)
(0.652, 0.176, 0.0) by 0.628319 radians = (0.527479, 0.176, 0.383236)
(0.368, 0.504, 0.0) by 0.628319 radians = (0.297718, 0.504, 0.216305)
(0.048, -0.296, 0.0) by 1.25664 radians = (0.0148328, -0.296, 0.0456507)
(0.376, -0.508, 0.0) by 1.25664 radians = (0.11619, -0.508, 0.357597)
(0.72, -0.204, 0.0) by 1.25664 radians = (0.222492, -0.204, 0.684761)
(0.652, 0.176, 0.0) by 1.25664 radians = (0.201479, 0.176, 0.620089)
(0.368, 0.504, 0.0) by 1.25664 radians = (0.113718, 0.504, 0.349989)
...
(0.048, -0.296, 0.0) by 6.28319 radians = (0.048, -0.296, -1.17566e-17)
(0.376, -0.508, 0.0) by 6.28319 radians = (0.376, -0.508, -9.20934e-17)
(0.72, -0.204, 0.0) by 6.28319 radians = (0.72, -0.204, -1.76349e-16)
(0.652, 0.176, 0.0) by 6.28319 radians = (0.652, 0.176, -1.59694e-16)
(0.368, 0.504, 0.0) by 6.28319 radians = (0.368, 0.504, -9.0134e-17)
I'm not sure what exactly is going on here, but I'm having a terrible time trying to figure it out, so please don't think I'm trying to get double reputation or anything, I'm just really stuck.
EDIT 2: Here is my whole display routine for my perspective subview:
void displayPersp(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
gluLookAt (-2.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0);
// draw the axis
glBegin(GL_LINES);
// x
glVertex3f(500.0, 0.0, 0.0);
glVertex3f(-500.0, 0.0, 0.0);
// y
glVertex3f(0.0, -500.0, 0.0);
glVertex3f(0.0, 500.0, 0.0);
// z
glVertex3f(0.0, 0.0, -500.0);
glVertex3f(0.0, 0.0, 500.0);
glEnd();
cout << endl;
// loop as many number of times as we are going to draw the points around the Y-Axis
for( int i=0; i<=sweepResolutionMod; i++ )
{
cout << endl;
// setup the new angle
double angle = i>0 ? (360/sweepResolutionMod)*i : 0;
angle = angle * (M_PI/180);
// for each point...
for( int i=0; i<clickedPoints.size(); i++ )
{
GLfloat tempX = (clickedPoints[i].x-250)/250;
GLfloat tempY = (clickedPoints[i].y-250)/250;
GLfloat tempZ = 0.0;
cout << "(" << tempX << ", " << tempY << ", 0.0) by " << angle << " degrees = ";
GLfloat newX = (tempX * cos(angle)) - (tempZ * sin(angle));
GLfloat newY = tempY;
GLfloat newZ = (tempX * sin(angle)) - (tempZ * cos(angle));
cout << "(" << newX << ", " << newY << ", " << newZ << ")\n";
glVertex3d(newX, newY, newZ);
}
// the following was my old solution, using OpenGL's rotate(), but that
// didn't allow me to get back the new point's coordinates.
/*
glRotatef(angle, 0.0, 1.0, 0.0);
// draw a line?
if( clickedPoints.size() > 1 )
{
glBegin(GL_LINE_STRIP);
for(int i=0; i<clickedPoints.size(); i++ )
{
glVertex3f((clickedPoints[i].x-250)/250, (clickedPoints[i].y-250)/250, 0.0);
}
glEnd();
}
// everyone gets points
glBegin(GL_POINTS);
for(int i=0; i<clickedPoints.size(); i++ )
{
glVertex3f((clickedPoints[i].x-250)/250, (clickedPoints[i].y-250)/250, 0.0);
}
glEnd();
*/
}
glutSwapBuffers();
}
EDIT 3: Here is a terrible illustration that illustrates what I need to do. I know the perspective seems off, but what I'm attempting to acquire is the green 'horizontals' in the right subview (this is using the commented out glRotatef() code above):
FINAL EDIT (for future generations!):
Here is what I finally got working, after discussing some linear algebra with a teacher at college:
void displayPersp(void)
{
glClear(GL_COLOR_BUFFER_BIT);
gluLookAt (-2.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
// draw the axis
glBegin(GL_LINES);
// x
glVertex3f(500.0, 0.0, 0.0);
glVertex3f(-500.0, 0.0, 0.0);
// y
glVertex3f(0.0, -500.0, 0.0);
glVertex3f(0.0, 500.0, 0.0);
// z
glVertex3f(0.0, 0.0, -500.0);
glVertex3f(0.0, 0.0, 500.0);
glEnd();
cout << endl;
double previousTheta = 0.0;
for( int i=0; i<=sweepResolutionMod; i++ )
{
double theta = i>0 ? (360/sweepResolutionMod)*i : 0;
theta = theta * (M_PI/180);
if( clickedPoints.size() > 1 )
{
// the 'vertical' piece
glBegin(GL_LINE_STRIP);
for(int i=0; i<clickedPoints.size(); i++ )
{
// normalize
GLfloat tempX = (clickedPoints[i].x-250)/250;
GLfloat tempY = (clickedPoints[i].y-250)/250;
GLfloat tempZ = 0.0;
// new points
GLfloat newX = ( tempX * cos(theta) ) + ( tempZ * sin(theta) );
GLfloat newY = tempY;
GLfloat newZ = ( tempZ * cos(theta) ) - ( tempX * sin(theta) );
glVertex3f(newX, newY, newZ);
}
glEnd();
// the 'horizontal' piece
if( previousTheta != theta )
{
glBegin(GL_LINES);
for(int i=0; i<clickedPoints.size(); i++ )
{
// normalize
GLfloat tempX = (clickedPoints[i].x-250)/250;
GLfloat tempY = (clickedPoints[i].y-250)/250;
GLfloat tempZ = 0.0;
// new points
GLfloat newX = ( tempX * cos(theta) ) + ( tempZ * sin(theta) );
GLfloat newY = tempY;
GLfloat newZ = ( tempZ * cos(theta) ) - ( tempX * sin(theta) );
// previous points
GLfloat previousX = ( tempX * cos(previousTheta) ) + ( tempZ * sin(previousTheta) );
GLfloat previousY = tempY;
GLfloat previousZ = ( tempZ * cos(previousTheta) ) - ( tempX * sin(previousTheta) );
// horizontal component
glVertex3f(newX, newY, newZ);
glVertex3f(previousX, previousY, previousZ);
}
glEnd();
}
}
previousTheta = theta;
}
glutSwapBuffers();
}
Edit 2: Okay, I see the problem you're running into -- it's a limitation I'd forgotten about (so the code I'd posted previously was dead wrong and wouldn't work at all). The problem is that you're not allowed to call glRotate between a glBegin/glEnd pair -- if you do, it'll set an error flag, and no more drawing will be done.
That does mean you pretty much have to handle the rotation yourself. Fortunately, that's a bit simpler than you've tried to make it:
static const double pi = 3.1416;
for (int point=0; point<NUM_POINTS; point++) {
glBegin(GL_LINE_STRIP);
for (double theta = 0.0; theta < 2.0 * pi; theta += pi/6.0) {
double x = cos(theta);
double z = sin(theta);
glVertex3d(points[point][0]*x, points[point][1], -1.0-points[point][0]*z);
}
glEnd();
}
As-is, this code uses -1.0 along the Z axis as the center of rotation. You can obviously move that where you wish, though anything outside your clipping frustum obviously won't display.
Also note that to get a wireframe, you'll have to draw both your "vertical", and your "horizontal" lines separately, so the code will look something like this:
for (int point=0; point<NUM_POINTS; point++) {
glBegin(GL_LINE_STRIP);
for (double theta = 0.0; theta < 2.0 * pi; theta += pi/6.0) {
double x = cos(theta);
double z = sin(theta);
glVertex3d(points[point][0]*x, points[point][1], -1.0 - points[point][0]*z);
}
glEnd();
}
for (double theta = 0.0; theta < 2.0 * pi; theta += pi/6.0) {
glBegin(GL_LINE_STRIP);
for (int point=0; point<NUM_POINTS; point++) {
double x = cos(theta);
double z = sin(theta);
glVertex3d(points[point][0]*x, points[point][1], -1.0 - points[point][0]*z);
}
glEnd();
}
The trig functions take angles in radians, not degrees.
I also suspect that your viewport isn't setup properly, which explains why you can't see anything on the screen. Typically when I think stuff isn't rendering, it usually is, however, I haven't configured the camera, lighting and other stuff correctly.
Looks like you're trying to construct a surface of revolution/solid of revolution/"lathe object".
A working example:
#include <GL/glut.h>
#include <glm/glm.hpp>
#include <vector>
#include <cmath>
using namespace std;
using namespace glm;
struct Vertex
{
Vertex( const vec3& position, const vec3& normal )
: position( position )
, normal( normal )
{}
vec3 position;
vec3 normal;
};
// spin the pts array around the Z axis.
// pts.x will become the radius, and pts.y will become the height
// pts should be sorted by y-coordinate
vector< Vertex > Lathe( const vector< vec2 >& pts, unsigned int segments = 32 )
{
// precalculate circle points
vector< vec2 > circlePts;
for( unsigned int i = 0; i <= segments; ++i )
{
float angle = ( i / (float)segments ) * 3.14159f * 2.0f;
circlePts.push_back( vec2( cos( angle ), sin( angle ) ) );
}
// fill each layer
typedef vector< vec3 > Layer;
typedef vector< Layer > Layers;
Layers layers( pts.size(), Layer( circlePts.size() ) );
for( size_t i = 0; i < pts.size(); ++i )
{
for( unsigned int j = 0; j < circlePts.size(); ++j )
{
layers[i][j] = vec3( circlePts[j] * pts[i].x, pts[i].y );
}
}
// move through layers generating triangles
vector< Vertex > verts;
for( size_t i = 1; i < layers.size(); ++i )
{
const Layer& prvLayer = layers[ i-1 ];
const Layer& curLayer = layers[ i-0 ];
for( size_t j = 1; j < circlePts.size(); ++j )
{
// upper = cur layer
// UL -- UR
// left | 0 / | right
// = j-1 | / 1 | = j-0
// LL -- LR
// lower = prv layer
const vec3& LL = prvLayer[ j-1 ]; // lower-left
const vec3& LR = prvLayer[ j-0 ]; // lower-right
const vec3& UL = curLayer[ j-1 ]; // upper-left
const vec3& UR = curLayer[ j-0 ]; // upper-right
// triangle0: LL -> UR -> UL
const vec3 normal0 = normalize( cross( UR - LL, UL - LL ) );
verts.push_back( Vertex( LL, normal0 ) );
verts.push_back( Vertex( UR, normal0 ) );
verts.push_back( Vertex( UL, normal0 ) );
// triangle1: LL -> LR -> UR
const vec3 normal1 = normalize( cross( LR - LL, UL - LL ) );
verts.push_back( Vertex( LL, normal1 ) );
verts.push_back( Vertex( LR, normal1 ) );
verts.push_back( Vertex( UR, normal1 ) );
}
}
return verts;
}
// mouse state
int btn;
ivec2 startMouse;
ivec2 startRot, curRot;
void mouse(int button, int state, int x, int y )
{
if( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN )
{
btn = button;
startMouse = ivec2( x, glutGet( GLUT_WINDOW_HEIGHT ) - y );
startRot = curRot;
}
}
void motion( int x, int y )
{
ivec2 curMouse( x, glutGet( GLUT_WINDOW_HEIGHT ) - y );
if( btn == GLUT_LEFT_BUTTON )
{
curRot = startRot + ( curMouse - startMouse );
}
glutPostRedisplay();
}
vector< Vertex > model;
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
double ar = w / h;
gluPerspective( 60, ar, 0.1, 40 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef( 0, 0, -10 );
glPushMatrix();
glRotatef( curRot.x % 360, 0, 1, 0 );
glRotatef( -curRot.y % 360, 1, 0, 0 );
// draw model
if( !model.empty() )
{
glColor3ub( 255, 0, 0 );
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_NORMAL_ARRAY );
glVertexPointer( 3, GL_FLOAT, sizeof(Vertex), &model[0].position );
glNormalPointer( GL_FLOAT, sizeof(Vertex), &model[0].normal );
glDrawArrays( GL_TRIANGLES, 0, model.size() );
glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_NORMAL_ARRAY );
}
// draw bounding cube
glDisable( GL_LIGHTING );
glColor3ub( 255, 255, 255 );
glutWireCube( 7 );
glEnable( GL_LIGHTING );
glPopMatrix();
glutSwapBuffers();
}
int main( int argc, char **argv )
{
vector< vec2 > pts;
pts.push_back( vec2( 0.1, -3 ) );
pts.push_back( vec2( 2, -2 ) );
pts.push_back( vec2( 3, -1 ) );
pts.push_back( vec2( 1, 0 ) );
pts.push_back( vec2( 3, 1 ) );
pts.push_back( vec2( 4, 2 ) );
pts.push_back( vec2( 4, 3 ) );
model = Lathe( pts );
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "GLUT" );
glutDisplayFunc( display );
glutMouseFunc( mouse );
glutMotionFunc( motion );
glEnable( GL_DEPTH_TEST );
// set up lighting
glShadeModel( GL_SMOOTH );
glEnable( GL_COLOR_MATERIAL );
glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ) ;
glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE );
glEnable( GL_LIGHTING );
// set up "headlamp"-like light
glEnable( GL_LIGHT0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
GLfloat position[] = { 0, 0, 1, 0 };
glLightfv( GL_LIGHT0, GL_POSITION, position );
glPolygonMode( GL_FRONT, GL_FILL );
glPolygonMode( GL_BACK, GL_LINE );
glutMainLoop();
return 0;
}
To me this should just work, so the fact it does not, almost certainly means I am the one in the wrong. Even though in principle a Transform< double, 3, Affine > is the same as a Matrix< double, 4, 4 >, they cannot be used together sensibly:
Affine3d rotMat( AngleAxisd( 45.0, ( Vector3d() << 0.0, 1.0, 0.0 ).finished() ) );
Matrix4d m;
m << 1.0, 0.0, 0.0, 6.0,
0.0, 1.0, 0.0, 6.0,
0.0, 0.0, 1.0, 6.0,
0.0, 0.0, 0.0, 1.0;
m = m * rotMat;
Results in a 'no match for operator=' error on the last line, and the in-place multiplication operator results in the same, trying to initialise a Matrix4d with Affine3d does not work either. Does anybody know how to actually use the Transform class in any useful way?
Thanks,
Cam
Just write:
m = m * rotMat.matrix();
I don't know if it is an oversight that Eigen doesn't define this multiplication implicitly or if it might interfere with other use cases of the library.