Matrix Scale & translation

Just a warning that this is for a class and is a lab for homework. I'm not expecting an answer but some help for going the right direction would be awesome. I have a class I've created called Vector3D it normally takes in a 3D vector. However we are now working with 4D vector's and I'm modifying the code to use array's of vectors to create a matrix and translate it.

I can get the array to carry over, i can modify the array and set it to a new array after the translation. However when it returns the array it doesn't modify anything. I'm not sure why this happening. I have my main cpp file and the functions I'm using from my Vector3D class.

main.cpp
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
void main()
{
	Vector3D* matrix;
	Vector3D* resultMatrix;
	int verticies;
	int vec;
	int vert = 1;
	float tmpX;
	float tmpY;
	float tmpZ;
	float tmpW;
	int choice = 0;
	bool goOn = true;
	bool validChoice = false;
	char userInput;

  cout << "Enter the amount of verticies for the object: ";
	cin >> verticies;
	matrix = new Vector3D[verticies];
  resultMatrix = new Vector3D[verticies];
	
	for (int i = 0; i < verticies; i++)
	{
		cout << "Please enter the X, Y, Z and W coordinates for the " << verticies << "X4 matrix:" << endl;
		cout << vert <<":   X: ";
    vert++;
		cin >> tmpX;
		cout << "     Y: ";
		cin >> tmpY;
		cout << "     Z: ";
		cin >> tmpZ;
		cout << "     W: ";
		cin >> tmpW;
		matrix[i].set4DVector(tmpX, tmpY, tmpZ, tmpW);
	}

	cout << "Enter the Translation you want to do ";
				cout << "X translation: ";
				cin >> tmpX;
				cout << "Y translation: ";
				cin >> tmpY;
				cout << "Z translation: ";
				cin >> tmpZ;
				cout << "W translation: ";
				cin >> tmpW;
				for (int i=0 ; i < verticies; i++)
				{
					resultMatrix[i].translation(&matrix[i], tmpX, tmpY, tmpZ, tmpW, verticies);
				}

cout << "The new vertices are: " << endl;
		for (int i = 0; i < verticies; i++)
		{
			cout << vec << ":     ";
			vec++;
			cout << "X: " << matrix[i].getX();
			cout << " Y: " << matrix[i].getY();
			cout << " Z: " << matrix[i].getZ();
			cout << " W: " << matrix[i].getW();
			cout << endl;
		}


And the vector3D.h file
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
#ifndef Vector3D_H
#define Vector3D_H
#include <cmath>
#include <iostream>

using namespace std;

class Vector3D
{
public:

void set4DVector(float theX, float theY, float theZ, float theW)
	{
		vecX = theX;
		vecY = theY;
		vecZ = theZ;
		vecW = theW;
	}

// Returns the X value as a float
	float getX()
	{
		return vecX;
	}

	// Returns the Y value as a float
	float getY()
	{
		return vecY;
	}

	// Returns the Z value as a float
	float getZ()
	{
		return vecZ;
	}

	// Returns the W value as a float
	float getW()
	{
		return vecW;
	}

float dotProduct(Vector3D a, Vector3D b) // *
  { 
	  return ((a.getX() * b.getX()) + (a.getY() * b.getY()) + (a.getZ() * b.getZ()) + (a.getW() * b.getW()));
  }

  void* translation(Vector3D a[], float theX, float theY, float theZ, float theW, int vert)
  {
   Vector3D * t;
   t = new Vector3D [3];
   t[0].set4DVector(1, 0, 0, theX);
   t[1].set4DVector(0, 1, 0, theY);
   t[2].set4DVector(0, 0, 1, theZ);
   t[3].set4DVector(0, 0, 0, 1);
   float newX= 1 , newY = 1, newZ = 1;
    int verticies = vert;
	  Vector3D* result; // resulting vector returns to stack
    
    result = new Vector3D[verticies];

    for (int i = 0; i < verticies; i++)
    {
    newX = newX + (t[0].dotProduct(t[0], a[i]));
    newY = newY + (t[1].dotProduct(t[1], a[i]));
    newZ = newZ + (t[2].dotProduct(t[2], a[i]));
    }
    for (int i = 0; i < verticies; i++)
    {
      result[i].set4DVector(a[i].getX() + newX,a[i].getY() + newY, a[i].getZ() + newZ, 1 );
    }
	  return result;
  }

private:
	float vecX, vecY, vecZ, vecW; // the different X, Y, and Z coordinates of the Vector.
	float tempDivideTop; // top value in any division
	float tempDivideBottom; // bottom value used to catch divide by 0 error
	
};
#endif 


Any help would be great on this.
If you know the size of your vector allocate it statically.
resultMatrix[i].translation(&matrix[i], tmpX, tmpY, tmpZ, tmpW, verticies); Don't catch return value. Expects to modify matrix.
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
  void* translation(Vector3D a[], float theX, float theY, float theZ, float theW, int vert)
  {
   Vector3D * t;
   t = new Vector3D [3]; //memory leak (replace by static allocation)
   t[0].set4DVector(1, 0, 0, theX);
   t[1].set4DVector(0, 1, 0, theY);
   t[2].set4DVector(0, 0, 1, theZ);
   t[3].set4DVector(0, 0, 0, 1);
   float newX= 1 , newY = 1, newZ = 1;
    int verticies = vert;
	  Vector3D* result; // local variable
    
    result = new Vector3D[verticies];

    for (int i = 0; i < verticies; i++)
    {
    newX = newX + (t[0].dotProduct(t[0], a[i]));
    newY = newY + (t[1].dotProduct(t[1], a[i]));
    newZ = newZ + (t[2].dotProduct(t[2], a[i]));
    }
    for (int i = 0; i < verticies; i++)
    {
      result[i].set4DVector(a[i].getX() + newX,a[i].getY() + newY, a[i].getZ() + newZ, 1 ); //modify local variable
    }
	  return result; //returning a value
  }
result is the matrix that has the translation, but you are not catching its value.

Another comments:
_ What is the purpose of resultMatrix in main? It's incredible big and filled with garbage.
_ Be careful with dynamic allocation (avoid it). Check for memory leaks.
_ Why do you use a member function (dotProduct) as it was a global? t[0].dotProduct(a[i]);
_ Those setters and getters are 'stupid'. For what they do, you better make your members public.
_ You don't use W in the translation, why ask for it? Let the client work with (x,y,z) and you perform the operations in the space (x,y,z,w)
_ What is the purpose of resultMatrix in main? It's incredible big and filled with garbage.

It was something i was trying so i would catch a result. it can be removed with little to no changes

_ Be careful with dynamic allocation (avoid it). Check for memory leaks.
dynamic allocation was something i've had some run in's with the teacher suggested using it.

_ Why do you use a member function (dotProduct) as it was a global? t[0].dotProduct(a[i]);
I'm not really sure on this.

_ Those setters and getters are 'stupid'. For what they do, you better make your members public.
Other functions in the class use those variables and modify the data (you're not seeing the full class, the forum won't allow me to post that class because it is to big. It's a 3D vector with all the math built in for modifying vectors.) For the assignment at hand those are fine.

_ You don't use W in the translation, why ask for it? Let the client work with (x,y,z) and you perform the operations in the space (x,y,z,w)
Again with the class we built we use W in other functions and will use it later in 3D rotations so i need to build in methods to get that value now.
So you want that resultMatrix has the translated points?
1
2
3
4
5
6
7
//main
Vector offset(tmpX, tmpY, tmpZ, tmpW);
for (int i=0 ; i < verticies; i++)
{
	//resultMatrix[i].translation(&matrix[i], tmpX, tmpY, tmpZ, tmpW, verticies);
	resultMatrix[i] = matrix[i].translation(offset);
}
1
2
3
4
5
//void* translation(Vector3D a[], float theX, float theY, float theZ, float theW, int vert)
Vector Vector::translation(const Vector &offset){
//t is an identity matrix, except for the last column that it is 'offset'
	return t * (*this);
}
Perform the translation one point at the time. The problem with this is that the t matrix is always the same, but it is created an destroyed in every iteration.
An easy way to fix that is to change the class responsible for doing the operation. A Matrix4D or Environment class could be useful. (check OpenGL: pushMatrix, rotate, translate, scale, etc.)
Last edited on
Topic archived. No new replies allowed.