I've been learning Visual C++ on the fly at work for the last 2 weeks and could use some help. I'm writing a program to read a .raw file which has a standard format of 9 columns and then depending on the size of the object, a large number of rows. I then need to rotate it and write the new position of the object to a new .raw file. I started with a box as a simple example that had only 28 rows. The code worked fine for the small number of rows, but the objects I'll be using have approx. 35000 rows and it's overloading the arrays "geom" and "geom_r". Any tips for me to get around this would be greatly appreciated.
Thanks,
Adam Bunn -
#include<iostream.h> //For input/output
-
#include<math.h> //For math functions
-
#include<stdio.h> //For fopen, FILE...
-
int main()
-
-
{
-
//Declaration of Variables
-
const float pi = 3.14159f;
-
float degrees = 45.0f;
-
float radians = degrees * pi / 180;
-
int i, j, k, p, q, r, s, c, d, w, lines;
-
float geom[28][9];
-
float geom_r[28][9];
-
float vertice1[3][1], vertice2[3][1], vertice3[3][1];
-
float r_vertice1[3][1], r_vertice2[3][1], r_vertice3[3][1];
-
float sum1, sum2, sum3;
-
float z_rotate[3][3] = {
-
{cos(radians), -sin(radians), 0}, //rotation matrices
-
{sin(radians), cos(radians), 0},
-
{0,0,1}
-
};
-
float y_rotate[3][3] = {
-
{cos(radians), 0, -sin(radians)},
-
{0,1,0},
-
{sin(radians), 0, cos(radians)}
-
};
-
-
FILE * praw; //Open file to read
-
praw = fopen("D:\\Adam\\Hip ROM Project\\C++\\figure_it_out\\box.raw", "r"); //File name!
-
-
//for (i = 0; i < 28; i++){ //Read file into box array
-
i = 0;
-
while (!feof(praw)) {
-
for (j = 0; j < 9; j++){
-
fscanf(praw, "%f", &geom[i][j]);
-
}
-
i = i +1;
-
}
-
fclose(praw);
-
-
lines = i - 1; //number of lines in the file
-
for (p = 0; p < lines; p++){ //Break the rows of the array into 3x1 vectors
-
r = 3; s = 6; //initialize counters
-
for (q = 0; q < 3; q++){
-
vertice1[q][0] = geom[p][q]; //First three columns into first vertice
-
vertice2[q][0] = geom[p][r]; //Second three columns into second vertice
-
vertice3[q][0] = geom[p][s]; //Third three columns into third vertice
-
r = r + 1; //increase counters
-
s = s + 1;
-
-
}
-
-
-
for(i=0;i<3;i++){ //Multiply matrices to rotate geometry
-
j = 0;
-
sum1 = 0; sum2 = 0; sum3 = 0;
-
for(k=0;k<3;k++){
-
sum1 = sum1 + (y_rotate[i][k] * vertice1[k][j]);
-
sum2 = sum2 + (y_rotate[i][k] * vertice2[k][j]);
-
sum3 = sum3 + (y_rotate[i][k] * vertice3[k][j]);
-
r_vertice1[i][j] = sum1;
-
r_vertice2[i][j] = sum2;
-
r_vertice3[i][j] = sum3;
-
}
-
-
}
-
for (w = 0; w < 3; w++){ //Store rotated vertices in new array, box_r
-
c = w +3; d = w + 6;
-
geom_r[p][w] = r_vertice1[w][0];
-
geom_r[p][c] = r_vertice2[w][0];
-
geom_r[p][d] = r_vertice3[w][0];
-
}
-
}
-
-
praw = fopen("D:\\Adam\\Hip ROM Project\\C++\\figure_it_out\\box_r.raw", "a"); //Write new array to new file by appending
-
for (i = 0; i < lines; i++){
-
for (j = 0; j < 9; j++){
-
fprintf(praw, "%f ", geom_r[i][j]);
-
}
-
fprintf (praw, "\n");
-
}
-
return 0;
-
}
-
7 2589
I've been learning Visual C++ on the fly at work for the last 2 weeks and could use some help. I'm writing a program to read a .raw file which has a standard format of 9 columns and then depending on the size of the object, a large number of rows. I then need to rotate it and write the new position of the object to a new .raw file. I started with a box as a simple example that had only 28 rows. The code worked fine for the small number of rows, but the objects I'll be using have approx. 35000 rows and it's overloading the arrays "geom" and "geom_r". Any tips for me to get around this would be greatly appreciated.
Thanks,
Adam Bunn
Do you know how to allocate dynamic memory?
Savage
I'm trying to wrap my head around it, as well as the use of pointers in general, but the book I've got doesn't give a good example of how to do things multi dimensionally.
I'm trying to wrap my head around it, as well as the use of pointers in general, but the book I've got doesn't give a good example of how to do things multi dimensionally.
When allocating multidimensional arrays on heap,you must first allocate array of pointers,and the for every pointer you allocate array of data that pointer can point to.In your float example that would be: - float **somefloat;
-
-
somefloat=new float*[elements in 1D];
-
for(i=0;i<1D;i++) somefloat[i]=new float[elements in 2D];
But when you are done with it you must free it using reverse process.
Savage
Other solution is to use STL's vectors,if you know how to use them,which manages its memory all by it self.
Savage
Thanks for the help. I was able to get that to work pretty easily. There is one other thing that you might be able to help me with. Is there a way to determine how many lines/rows the .raw file has before I set how much memory is dynamically allocated? Or should I just set the array row size high enough that I won't have to worry about a file approaching the max size? If you could point me in the right direction I'd be very greatful. I changed my code to read like this now: -
float z_rotate[3][3] = {
-
{cos(radians), -sin(radians), 0}, //rotation matrices
-
{sin(radians), cos(radians), 0},
-
{0,0,1}
-
};
-
float y_rotate[3][3] = {
-
{cos(radians), 0, -sin(radians)},
-
{0,1,0},
-
{sin(radians), 0, cos(radians)}
-
};
-
-
geom = new float*[40000]; //Dynamic memory allocation
-
for (i = 0; i<40000; i++) geom[i] = new float[9];
-
geom_r = new float*[40000];
-
for (i = 0; i<40000; i++) geom_r[i] = new float[9];
-
-
FILE * praw; //Open file to read
-
Everything else is the same until I delete the arrays near the end. Thanks again
Thanks for the help. I was able to get that to work pretty easily. There is one other thing that you might be able to help me with. Is there a way to determine how many lines/rows the .raw file has before I set how much memory is dynamically allocated? Or should I just set the array row size high enough that I won't have to worry about a file approaching the max size? If you could point me in the right direction I'd be very greatful. I changed my code to read like this now: -
float z_rotate[3][3] = {
-
{cos(radians), -sin(radians), 0}, //rotation matrices
-
{sin(radians), cos(radians), 0},
-
{0,0,1}
-
};
-
float y_rotate[3][3] = {
-
{cos(radians), 0, -sin(radians)},
-
{0,1,0},
-
{sin(radians), 0, cos(radians)}
-
};
-
-
geom = new float*[40000]; //Dynamic memory allocation
-
for (i = 0; i<40000; i++) geom[i] = new float[9];
-
geom_r = new float*[40000];
-
for (i = 0; i<40000; i++) geom_r[i] = new float[9];
-
-
FILE * praw; //Open file to read
-
Everything else is the same until I delete the arrays near the end. Thanks again
Yes,there are varous ways to do that.One is to get size of file and size of single line(there are 9 columns so each row must have same size),and other is a simple while loop,while EOF is not reached read in a line and increase number of lines counter
Savage
Thanks for your help. I got it wrapped up.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Nanda |
last post by:
hi,
I am trying to generate parameters for the updatecommand
at runtime.
this.oleDbDeleteCommand1.CommandText=cmdtext;
this.oleDbDeleteCommand1.Connection =this.oleDbConnection1;...
|
by: Oenone |
last post by:
Is it possible to create an object which can have methods and properties,
but which can also be treated as a string?
I'm trying to create a wrapper around the IIS Request.Form object which...
|
by: stealth_spoof |
last post by:
Hi People wondering if anyone can help me with a problem I'm having
I'm trying to create an array with an unspecified length, the length is
based on the result i get from another task in the code...
|
by: djhulme |
last post by:
Hi,
I'm using GCC. Please could you tell me, what is the maximum number of
array elements that I can create in C, i.e.
char* anArray = (char*) calloc( ??MAX?? , sizeof(char) ) ;
I've...
|
by: arne.muller |
last post by:
Hello,
I've come across some problems reading strucutres from binary files.
Basically I've some strutures
typedef struct {
int i;
double x;
int n;
double *mz;
|
by: lawpoop |
last post by:
Hello!
I am working on a map of a rather large php project that I've been
working on. I hope to create a map of the files of the project that
would look like the output of the unix 'tree'...
|
by: Aryan |
last post by:
Hi,
I am using C# with framework 2.0 and creating PDF files on-fly, along
with this I am using Windows 2003 Server.
I am using Byte to take the data input and then save into pdf format
on...
|
by: =?Utf-8?B?U3RldmVa?= |
last post by:
First off, I am not sure if this belongs in this group or the C# group. It
seems more like a C++ problem to me. Anyways...
I have a C# project which links in an unmanaged C++ DLL, both built...
|
by: hamishd |
last post by:
What is the best way to store large arrays of numbers (eg, 4-byte
integers)?
Say I want to store an array of 1billion 4-byte integers.. If my
computer has 4gB memory, then is this possible?
...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |