Is this Pascalian pseudocode a 2d array or a 1D array?
var T: array[1..n, 1..p] of vartype
It's 2D, the same as
var T:array[1..n]of array[1..p]of vartype;
Related
std::vector<int> v(6);
how to declare a two dimensional vector with limits like above code for one dimension
I'm a noob in c++. I tried like this:
`
std::vector<int> v(6)(2);
`
I expected a two dimensional vector with 6 rows and 2 columns to take input in.
I know how to declare 2d vector. I just wanted it with limit.
In C++, there's no direct type which exactly represents a 2D "vector"/"matrix"/"tensor".
What you can create, is a vector of vectors. You'd write that as std::vector<std::vector<int>> - each element of the outer vector is itself a vector. But there's an important difference here: each of the inner vectors has its own length, and those could be different.
vector has a constructor taking a second argument, the initial value. You can use that here to initialize the inner vectors:
std::vector<std::vector<int>> v(6, std::vector<int>(2));
I did write this code and i got the message below
x = np.reshape(x,(1235,3))
Value Error: cannot reshape array of size 1235 into shape (1235,3)
I have implemented a polygon triangulation algorithm into my program. The algorithm first takes a simple polygon, described as 2D points/vertices, and splits it into y-monotone pieces.
After this is done, the algorithm then takes each monotone polygon piece and splits it into triangular pieces.
The input to the algorithm I need help with is an array of vertices outlining a y-monotonic polygon in clockwise or counter-clockwise order. The output is a collection of all edges, both from the original polygon but also the new ones added by the triangulation algorithm in order to split this y-monotonic piece into triangular pieces. This works great if I only want to paint the result as it's possible to just draw each edge.
However, there is no particular order to this collection of edges, and I need the output to be an array of type triangle strip or an array where each triangle is simply described by it's 3 vertices (Example: [a1,a2,a3,b1,b2,b3] as we have triangle a and triangle b).
Is there any conventional algorithm or any other way which can help me solve this? Speed is not of extreme importance but I'd prefer a fast solution.
Here is a pseudocode example of the program used in order to give a better understanding of my question:
class Vertex {
var point;
var index;
init(x,y,index){
point = (x,y)
self.index = index
}
}
class Edge {
var from; // of type Vertex
var to; // of type Vertex
init(from, to){
self.from = from
self.to = to
}
}
A call to a function getMonotonePiecesOfPolygon(polygon) could generate one of many monotonic polygon pieces that would have these vertices and edges:
var vertex0 = Vertex(x0,y0,0)
var vertex1 = Vertex(x1,y1,1)
var vertex2 = Vertex(x2,y2,2)
var vertex3 = Vertex(x3,y3,3)
var edge0 = Edge(vertex0, vertex1)
var edge1 = Edge(vertex1, vertex2)
var edge2 = Edge(vertex2, vertex3)
var edge3 = Edge(vertex3, vertex4)
This could correspond to a rectangle like this:
edge0
0------1
| |
edge3 | | edge1
| |
3------2
edge2
Then, we can supply these edges in an array and divide the polygon into triangles with the a triangulation method 'makeMonotonePolygonIntoTrianglePieces()'
var edgesArray = [edge0,edge1,edge2,edge3]
var edgesArray = makePolygonIntoTrianglePieces(edgesArray)
Now the edgesArray could look like this:
edgesArray == [edge0,edge1,edge2,edge3,newEdge1]
where
newEdge1.from == vertex0
newEdge1.to == vertex2
which would correspond to a polygon like this if we were to draw each edge:
edge0
0------1
|\ |
edge3 | \ | edge1
| \ |
3------2
edge2
Now here is the part of what my question is seeking to solve. I need an array which would look like this:
var triangles = [
vertex0,vertex3,vertex2,
vertex0,vertex2,vertex1
]
or using triangle strips (each triangle described by one new vertex and two vertices from the previous triangle):
var triangles = [
vertex3, vertex2, vertex0,
vertex1
]
In Chapter 2 of the book you refer to in one of the comments, they present a data structure called Doubly-Connected Edge List (DCEL). DCEL:s are used to store planar subdivisions such as triangulations. From what you write, I think it could be what you are looking for.
https://en.wikipedia.org/wiki/Doubly_connected_edge_list
I am creating a program to render 3D graphics. I have a 3D array 'shapes' which contains all of the polygons to render. It is an array of polygons, where each polygon is itself an array of points, and each point is an array of 3 integer values (x, y, z co-ordinates). I have tried and failed to use the append() function. How else can I get it to work?
I've tried using the append() function, but this seems to not work with multidimensional arrays.
int[][][] addPolyhedron(int[][][] shapes, int[][][] polyhedron)
{
for(int i = 0; i < polyhedron.length; i ++)
{
shapes = append(shapes, polyhedron[i]);
{
return shapes;
}
I wanted this to extend the array shapes to include all of the polygons in the array polyhedron. However, I receive an error message saying 'type mismatch, "java.lang.Object" does not match with "int[][][]".' Thanks in advance.
In Java, arrays (of any dimension) are not extendable - the size is defined, allocated and fixed upon instantiation. You want to add to (and therefore dynamically resize) shapes. Although Processing does provide the append() function, I think it is more appropriate to use the ArrayList built-in Java data type.
Your function could be refactored into something like this:
ArrayList<Integer[][]> addPolyhedron(ArrayList<Integer[][]> shapes, ArrayList<Integer[][]> polyhedron)
{
shapes.addAll(polyhedron);
return shapes;
}
Note that int[][] has become Integer[][] because an ArrayList cannot be declared with primitive types (int, bool, float, etc.).
Adding an individual program-defined polygon to shapes would be done like this:
shapes.add(new Integer[][] {{1,2,5},{3,4,5},{6,5,4}}); // adds a triangle to shapes.
Getting coordinates from the shapes ArrayList would be done like this:
shapes.get(0)[1][1]; // returns 4.
I am trying to create a 3D array of size 1000x1000x1000 with all the elements (corresponding to voxels) being zero and then assign a random value in the 2000 to 2001 range instead of 0 to some specific elements in the array and finally store it as a binary file.
The array named "coord" is the Nx3 matrix coordinates (x,y,z) of the points that I need them to be assigned the random value in the 3D array.))
I should mention that all the x,y,z values of the coordinate matrix are floating point numbers with: 0<=x<=1000 0<=y<=1000 0<=z<=1000
My aim is to export the 3D matrix in a binary format (other than MATLAB's default binary format) so that I can use it with other programs.
Here is what I've been up to so far:
load coord;
a=coord(:,1);
b=coord(:,2);
c=coord(:,3);
d=rand(1000,1)*2000;
dd = 0:2:1000;
[xq,yq,zq] = meshgrid(dd,dd,dd);
vq = griddata3(a,b,c,d,xq,yq,zq,'nearest');
h=figure;
plot3(a,b,c,'ro')
%=========================================%
fid=fopen('data.bin','w');
fwrite(fid,vq,'single');
fclose(fid);
In the above code a, b and c are the coordinates of each point and d is the corresponding intensity values for the desired range. While it is possible to create a 3D mesh (using meshgrid) and then interpolate the intensity values for mesh points (using griddata3), the final result (vq) would not be the actual points (ai,bi,ci) and corresponding intensities , but rather an interpolated set of points which is pretty useful for visualization purposes (for instance if you like to fit a 3D surface which fits through actual data).
I am simply trying to find a way to store the actual data-points and their intensities into a file and export it.
Any help is highly appreciated.
If you want to save to files that will allow importing into a visualization software, a series of Tiff files will most likely be convenient, i.e.
maxValue = 2000; % this is the maximum signal that can possibly occur
% according to your code
for z = 1:size(vq,3)
%# convert slice z to 16 bit
currentSlice = vq(:,:,z);
currentSlice = uint16(round(currentSlice/maxValue))
%# save to file
imwrite(currentSlice, sprintf('testImg_z%04i.tif',z),'tif');
end
Note that if you create a double array of dimensions 1000x1000x1000, you'll need 8GB of contiguous RAM.
How about something like:
%# 3D array
voxels = zeros([1000 1000 1000]);
%# round points coordinates, and clamp to valid range [1,1000]
load coords
coords = round(coords);
coords = min(max(coords,1),1000);
%# convert to linear indices
idx = sub2ind(size(voxels), coords(:,1), coords(:,2), coords(:,3));
%# random values in the 2000 to 2001 range
v = rand(size(idx)) + 2000;
%# assign those values to the chosen points
voxels(idx) = v;