GPC Polygon Initialization - c

I am using the GPC Polygon Clipping lib and want to create a polygon programatically. I only see code for how to create one from a file. How can I do the initialization in my code?

Read better from your link, find the doc page and read; in particular gpc_add_contour function is likely what you need. The struct gpc_vertex_list holds a pointer to gpc_vertex-s and the number of vertex, and is what you must fill in. Like
gpc_polygon p = {0, NULL, NULL}; // "void" polygon
gpc_vertex v[] = { {0.0, 0.0}, {10.0, 0.}, {10.0, 10.10}, {0.0, 10.0} };
gpc_vertex_list vl = {
4, v
};
//...
gpc_add_contour(&p, &vl, 0);
The doc is not too much clear, but you can deduce the use, and testing (try-error loops) is your friend (I won't install gpc to do it anyway, so my code could be wrong). The proposed code snippet should create a square. Several other gpc_add_countour with the same &p but different vertex list can be used to create a more complex polygon, and of course vl can be changed to have at the beginning a more complex polygon. The third parameter should be 1 if you want the defined contour to be a "hole" in the current (p) polygon.

gpc_polygon subject;
int w = 100, h = 100, verticesCnt = 30;
//setup a gpc_polygon container and fill it with random vertices ...
subject.num_contours = 1;
subject.hole = 0;
subject.contour = new gpc_vertex_list; //ie just a single polygon here
subject.contour->num_vertices = verticesCnt;
subject.contour->vertex = new gpc_vertex [verticesCnt];
for (i = 0; i < verticesCnt; i++){
subject.contour[0].vertex[i].x = random(w);
subject.contour[0].vertex[i].y = random(h);
}
//do stuff with it here, then ...
gpc_free_polygon(&subject);

Related

Maya Mel Script - How to get bottom point of model?

How to get bottom point of model via Mel script?
If I can get the Y-coordinate of the bottom point, I can get the point of the bottom, but I can't figure out how to get it.
Looping through all vertices can be slow, especially with a dense mesh. What can be done instead is to use the object's bounding box:
float $bb[] = `getAttr pCube1.boundingBoxMin`;
print($bb[1]); // Lowest position in Y
Another benefit of this is that you aren't relying on vertices, so it doesn't have to be a polygonal mesh.
If I can get the Y-coordinate of the bottom point, I can get the point of the bottom, but I can't figure out how to get it.
I figure it out: Y-coordinate of the bottom point = Y-coordinate of the lowest vertex of the model. So do it like this: Loop through all vertices to get the lowest vertex.
Assume the target object is: "pCube1". Here is the code to get the lowest Y.
int $vtxIdx;
int $vCount[] = `polyEvaluate -vertex pCube1`; //Get vertex count
float $lowestY = 2147483647.0;
float $crtY = 0.0;
for ($vtxIdx = 0; $vtxIdx < $vCount[0]; $vtxIdx++)//Loop through vetex
{
float $pos[] = `xform -q -ws -t ("pCube1.vtx["+$vtxIdx+"]")`;//Get vertex position
$crtY = $pos[1];
if($crtY < $lowestY)
{
$lowestY = $crtY;//Get the lowest Y
}
}
print ($lowestY);
So the lowest point = (pCube1.X, lowestY, pCube1.Z).

Dereference UnsafeMutablePointer<UnsafeMutableRawPointer>

I have a block that is passing data in that I'd like to convert to an array of array of floats -- e.g. [[0.1,0.2,0.3, 1.0], [0.3, 0.4, 0.5, 1.0], [0.5, 0.6, 0.7, 1.0]]. This data is passed to me in the form of data:UnsafeMutablePointer<UnsafeMutableRawPointer> (The inner arrays are RGBA values)
fwiw -- the block parameters are from SCNParticleEventBlock
How can I dereference data into a [[Float]]? Once I have the array containing the inner arrays, I can reference the inner array (colorArray) data with:
let rgba: UnsafeMutablePointer<Float> = UnsafeMutablePointer(mutating: colorArray)
let count = 4
for i in 0..<count {
print((rgba+i).pointee)
}
fwiw -- this is Apple's example Objective-C code for referencing the data (from SCNParticleSystem handle(_:forProperties:handler:) )
[system handleEvent:SCNParticleEventBirth
forProperties:#[SCNParticlePropertyColor]
withBlock:^(void **data, size_t *dataStride, uint32_t *indices , NSInteger count) {
for (NSInteger i = 0; i < count; ++i) {
float *color = (float *)((char *)data[0] + dataStride[0] * i);
if (rand() & 0x1) { // Switch the green and red color components.
color[0] = color[1];
color[1] = 0;
}
}
}];
You can actually subscript the typed UnsafeMutablePointer without having to create an UnsafeMutableBufferPointer, as in:
let colorsPointer:UnsafeMutableRawPointer = data[0] + dataStride[0] * i
let rgbaBuffer = colorsPointer.bindMemory(to: Float.self, capacity: dataStride[0])
if(arc4random_uniform(2) == 1) {
rgbaBuffer[0] = rgbaBuffer[1]
rgbaBuffer[1] = 0
}
Were you ever able to get your solution to work? It appears only a handful of SCNParticleProperties can be used within an SCNParticleEventBlock block.
Based on this answer, I've written the particle system handler function in swift as:
ps.handle(SCNParticleEvent.birth, forProperties [SCNParticleSystem.ParticleProperty.color]) {
(data:UnsafeMutablePointer<UnsafeMutableRawPointer>, dataStride:UnsafeMutablePointer<Int>, indicies:UnsafeMutablePointer<UInt32>?, count:Int) in
for i in 0..<count {
// get an UnsafeMutableRawPointer to the i-th rgba element in the data
let colorsPointer:UnsafeMutableRawPointer = data[0] + dataStride[0] * i
// convert the UnsafeMutableRawPointer to a typed pointer by binding it to a type:
let floatPtr = colorsPointer.bindMemory(to: Float.self, capacity: dataStride[0])
// convert that to a an UnsafeMutableBufferPointer
var rgbaBuffer = UnsafeMutableBufferPointer(start: floatPtr, count: dataStride[0])
// At this point, I could convert the buffer to an Array, but doing so copies the data into the array and any changes made in the array are not reflected in the original data. UnsafeMutableBufferPointer are subscriptable, nice.
//var rgbaArray = Array(rgbaBuffer)
// about half the time, mess with the red and green components
if(arc4random_uniform(2) == 1) {
rgbaBuffer[0] = rgbaBuffer[1]
rgbaBuffer[1] = 0
}
}
}
I'm really not certain if this is the most direct way to go about this and seems rather cumbersome compared to the objective-C code (see above question). I'm certainly open to other solutions and/or comments on this solution.

How to create a world map using C language?

How to create a world map using C language?
Here, I want to generate a 1000*1000 two dimensional array of world map. Within the array, the land part is marked with value 1 and the sea part is marked with value 0. Is there any simple way to create?
Obviously, there's no algorithm which describes the shape of the coastline of the continents ;)
But you could use e.g. Smrender (http://www.abenteuerland.at/smrender/), feed it with the coastline of e.g. OpenStreetMap or Naturalearthdata and a single rule. Let it create a PNG image with 1000x1000 pixels.
EDIT:
With ImageMagick (convert) you can directly convert a PNG into a C header file as an array.
Go to Openstreetmap.org (or Googlemaps), zoom out until you see the whole world, make a screenshot, open it in your favorite image manipulation program and cut and resize it to 1000x1000, then run convert input.png output.h.
Bernhard
I wrote a answer in game development some hours ago that can help you, heres the topic, it's written in javascript, but it won't be hard to translate to c.
In the example you fill the whole map of 0's and then you trace a path randomly, if you want to smooth the map later you can make a snippet that loops all the watter tiles and converts them to land if there are 3 or more land tiles adjacent to it, do it 40-60 times and you will get a smoother shore and no "holes" in the continent.
EDIT
Updating the answer with a demo in C.
I've not tested it, but if you include the headers and checks the code, this is the answer.
//Create and populate the map
int mapsize = 100;
int map[mapsize*mapsize];
for(int t = 0; t < mapsize*mapsize; t++) map[t] = 0;
//make the path
int currPos[2] = {0,50};
map[currPos[0]+(currPos[1]*mapSize)] = 1;
int landTiles = 20000;
for(var l = 0; l < landTiles; l++){
int dir[2] = {RANDOM_BETWEEN_-1_AND_1, RANDOM_BETWEEN_-1_AND_1};
int next[2] = {currPos[0]+dir[0], currPos[1]+dir[1]};
map[next[0]+(next[1]*mapSize)] = 1;
currPos = next;
}
//Draw the map
for(var row = 0; row < mapSize; row++){
for(var col = 0; col < mapSize; col++){
cout << map[col+(row*mapSize)];
}
cout << endl;
}
You could have some program or script (taking as input some representation of some image of the Earth) which generates a long C file like
const char map[1000][1000] = {
{'0', '1', //.... etc for the first fow
},
{'0', '0', '0', '1', //... etc
}
/// etc for other rows
};
See also XBM for an example.

Index expression must be constant - WebGL/GLSL error

I'm having trouble accessing an array in a fragment shader using a non-constant int as the index. I've removed the formula as it wouldn't make much sense here anyway, but my code is meant to calculate the tileID based on the current pixel and use that to determine the color.
Here's my code:
int tileID = <Insert formula here>;
vec3 colorTest;
int arrayTest[1024];
for (int x = 0; x < 1024; x++) {
if (x == 1) arrayTest[x] = 1;
else arrayTest[x] = 2;
}
if (arrayTest[tileID] == 1) colorTest = vec3(0.0, 1.0, 0.0);
else if (arrayTest[tileID] == 2) colorTest = vec3(1.0, 0.0, 0.0);
else colorTest = vec3(0.0, 0.0, 0.0);
Apparently GLSL doesn't like this and I get the error:
'[]' : Index expression must be constant
Does anyone know how I would fix this? Thanks.
As background -- GLSL looks a lot like C, but compiles a bit different. Things are very unrolled, and conditionals may be executed in parallel and switched at the end, that sort of thing. Depends on the hardware...
You can use loop indices or constants to index into arrays. The assignment in your loop is ok, but the access by tileID isn't.
WebGL Shader language is from GLES, documented
http://www.khronos.org/registry/gles/specs/2.0/GLSL_ES_Specification_1.0.17.pdf
The Appendix, section 5, discusses:
Indexing of Arrays, Vectors and Matrices
Definition:
constant-index-expressions are a superset of constant-expressions. Constant-index-expressions can include loop indices as defined in Appendix A section 4.
The following are constant-index-expressions:
• Constant expressions
• Loop indices as defined in section 4
• Expressions composed of both of the above
When used as an index, a constant-index-expression must have integral type.
Hope that helps!
Oh, as for fixing it, in the exact example above... looks like you could compute from tileID rather than precompute and index.
Or, precompute whatever array you like, and pass it in as a texture. A texture, of course, can be indexed however you like.
Here's a javascript helper method I use, to pass floats down to the shaders:
function glSetupStuff() { ...
...
if(!gl.getExtension("OES_texture_float")) // <<-- enables RGBA float values, handy!
alert("cant pass in floats, use 8-bit values instead.");
... }
/*
* Pass in an array of rgba floats,
* for example: var data = new Float32Array([0.1,0.2,0.3,1, .5,.5,1.0,1]);
*/
function textureFromFloats(gl,width,height,float32Array)
{
var oldActive = gl.getParameter(gl.ACTIVE_TEXTURE);
gl.activeTexture(gl.TEXTURE15); // working register 31, thanks.
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA,
width, height, 0,
gl.RGBA, gl.FLOAT, float32Array);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.bindTexture(gl.TEXTURE_2D, null);
gl.activeTexture(oldActive);
return texture;
}
Note the use of gl.NEAREST, so it doesn't "blur" your values! Then you can set it up before the gl.drawXxx call, with something like
textureUnit = 3; // from 0 to 15 is ok
gl.activeTexture(gl.TEXTURE0 + textureUnit);
gl.bindTexture(gl.TEXTURE_2D, texture);
var z = gl.getUniformLocation(prog, "uSampler");
gl.uniform1i(z, textureUnit);
And in the shader (I believe fragment or vertex; some earlier webgl's didn't support vertex textures...)
uniform sampler2D uSampler;
...
vec4 value = texture2D(uSampler, vec2(xValueBetween0And1,yValueBetween0And1));
So, you have to index appropriately for the array-as-texture size, within range of 0 to 1. Try to sample from the middle of each value/pixel. Like, if the array is 2 values wide, index by 0.25 and 0.75.
That's the gist of it!
Tested in Safari 9.1.2 on OS X 10.11.6
uniform float data[32];
float getData(int id) {
for (int i=0; i<32; i++) {
if (i == id) return data[i];
}
}
void main(void) {
float f = getData(yourVariable);
}
I hit this error because I was attempting to use an integer variable to take the nth texture from an array of textures:
// this doesn't compile!
varying vec2 vUv; // uv coords
varying float vTexture; // texture index in array of textures
uniform sampler2D textures[3]; // identify that there are 3 textures
void main() {
int textureIndex = int(floor(vTexture));
gl_FragColor = texture2D(textures[textureIndex], vUv);
}
The solution was to break out the texture indexing into a sequence of conditionals:
// this compiles!
varying vec2 vUv; // uv coords
varying float vTexture; // texture index in array of textures
uniform sampler2D textures[3]; // identify that there are 3 textures
void main() {
int textureIndex = int(floor(vTexture));
if (textureIndex == 0) {
gl_FragColor = texture2D(textures[0], vUv);
} else if (textureIndex == 1) {
gl_FragColor = texture2D(textures[1], vUv);
} else if (textureIndex == 2) {
gl_FragColor = texture2D(textures[2], vUv);
}
}

How to draw a polygon from a set of unordered points

Currently, I am using a convex hull algorithm to get the outer most points from a set of points randomly placed. What I aim to do is draw a polygon from the set of points returned by the convex hull however, when I try to draw the polygon it looks quite strange.
My question, how do I order the points so the polygon draws correctly?
Thanks.
EDIT:
Also, I have tried sorting using orderby(...).ThenBy(...) and I cant seem to get it working.
Have you tried the gift wrapping algorithm ( http://en.wikipedia.org/wiki/Gift_wrapping_algorithm)? This should return points in the correct order.
I had an issue where a random set of points were generated from which a wrapped elevation vector needed a base contour. Having read the link supplied by #user1149913 and found a sample of gift-wrapping a hull, the following is a sample of my implementation:
private static PointCollection CalculateContour (List<Point> points) {
// locate lower-leftmost point
int hull = 0;
int i;
for (i = 1 ; i < points.Count ; i++) {
if (ComparePoint(points[i], points[hull])) {
hull = i;
}
}
// wrap contour
var outIndices = new int[points.Count];
int endPt;
i = 0;
do {
outIndices[i++] = hull;
endPt = 0;
for (int j = 1 ; j < points.Count ; j++)
if (hull == endPt || IsLeft(points[hull], points[endPt], points[j]))
endPt = j;
hull = endPt;
} while (endPt != outIndices[0]);
// build countour points
var contourPoints = new PointCollection(points.Capacity);
int results = i;
for (i = 0 ; i < results ; i++)
contourPoints.Add(points[outIndices[i]]);
return contourPoints;
}
This is not a full solution but a guide in the right direction. I faced a very similar problem just recently and I found a reddit post with an answer (https://www.reddit.com/r/DnDBehindTheScreen/comments/8efeta/a_random_star_chart_generator/dxvlsyt/) suggesting to use Delaunay triangulation which basically returns a solution with all possible triangles made within the data points you have. Once you have all possible triangles, which by definition you know won't result on any overlapped lines, you can chose which lines you use which result on all nodes being connected.
I was coding my solution on python and fortunately there's lots of scientific libraries on python. I was working on a random sky chart generator which would draw constellations out of those stars. In order to get all possible triangles (and draw them, just for fun), before going into the algorithm to draw the actual constellations, all I had to do was this:
# 2D array of the coordinates of every star generated randomly before
points = list(points_dict.keys())
from scipy.spatial import Delaunay
tri = Delaunay(points)
# Draw the debug constellation with the full array of lines
debug_constellation = Constellation(quadrants = quadrants, name_display_style = config.constellation_name_display_style)
for star in available_stars:
debug_constellation.add_star(star)
for triangle in tri.simplices:
star_ids = []
for index in triangle:
star_ids.append(points_dict[points[index]].id)
debug_constellation.draw_segment(star_ids, is_closed = True)
# Code to generate the image follows below
You can see the full implementation here: fake_sky_chart_generator/fake_libs/constellation_algorithms/delaunay.py
This is the result:

Resources