Skip to content

Commit

Permalink
v1.11 export fix
Browse files Browse the repository at this point in the history
  • Loading branch information
asjadnaqvi committed Mar 6, 2022
1 parent 67c2138 commit 53874e8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 39 deletions.
7 changes: 1 addition & 6 deletions installation/delaunay.ado
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ prog def delaunay, eclass sortpreserve
[ REScale TRIangles Hull VORonoi(namelist min=1 max=2) ] [OFFset(real 0.05)]




// check gtools
capture findfile gtools.ado
if _rc != 0 {
Expand All @@ -45,9 +43,6 @@ prog def delaunay, eclass sortpreserve
}





di _newline
di in yellow "Delaunay: Initializing >"

Expand Down Expand Up @@ -1016,7 +1011,7 @@ program define add_triangles, sortpreserve
}


*mata: st_matrix("triangles", mytriangles)
mata: st_matrix("triangles", mytriangles)
*mat colnames triangles = "tri_num" "tri_id" "tri_x" "tri_y"
*qui svmat triangles, n(col)
lab var tri_num "Triangle: number"
Expand Down
64 changes: 31 additions & 33 deletions installation/voronoi.ado
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ program define voronoi, eclass sortpreserve

// di "Voronoi: Initializing"


mata: vorlines = .
mata: vorpolygons = .

mata: voronoi_core(triangles, points, points2, halfedges, hull, `offset', vorlines, vorpolygons)
// di "Voronoi: Done with Mata routines"




// push to Stata

if "`lines'" != "" {
Expand Down Expand Up @@ -101,7 +105,6 @@ function voronoi_core(triangles, points, points2, halfedges, hull, offs, vorl, v
tri3 = colshape(triangles',3)' // reshape triangles



xmin = .
xmax = .
ymin = .
Expand All @@ -117,10 +120,7 @@ function voronoi_core(triangles, points, points2, halfedges, hull, offs, vorl, v
for (i=1; i <= num2; i++) {
vorcenter[i,.] = circumcenter2(points2,triangles,i)
}





// triangle pairs from which the center points are extracted (point0, point1)
voredges = J(rows(triangles),2,.) // voronoi edge pairs indexed to vorcenter
forEachVoronoiEdge(triangles,halfedges,voredges)
Expand Down Expand Up @@ -225,10 +225,11 @@ function voronoi_core(triangles, points, points2, halfedges, hull, offs, vorl, v

// clip the rays
// gen x1, y1, x2, y2
cliplist = J(rows(point0),4,.)
vorl = J(rows(point0),4,.)

for (i=1; i <= rows(point0); i++) {
//i
cliplist[i,.] = clipline(point0[i,1], point0[i,2], point1[i,1], point1[i,2], xmin, xmax, ymin, ymax)
vorl[i,.] = clipline(point0[i,1], point0[i,2], point1[i,1], point1[i,2], xmin, xmax, ymin, ymax)

}

Expand All @@ -240,14 +241,14 @@ function voronoi_core(triangles, points, points2, halfedges, hull, offs, vorl, v
// this is some error in the clipline. it is evaluating a wrong point on the line which should be dropped.
// manually drop them now. fix later

for (i=1; i <= rows(cliplist); i++) {
for (i=1; i <= rows(vorl); i++) {

if (cliplist[i,1]==cliplist[i,3] & cliplist[i,2]==cliplist[i,4]) {
if (vorl[i,1]==vorl[i,3] & vorl[i,2]==vorl[i,4]) {

cliplist[i,1] = .
cliplist[i,2] = .
cliplist[i,3] = .
cliplist[i,4] = .
vorl[i,1] = .
vorl[i,2] = .
vorl[i,3] = .
vorl[i,4] = .
}
}

Expand All @@ -261,16 +262,15 @@ function voronoi_core(triangles, points, points2, halfedges, hull, offs, vorl, v


// rescale back from [0,1] to the original domain
cliplist[.,1] = rescale2(cliplist[.,1], xmin, xmax, xminr, xmaxr)
cliplist[.,2] = rescale2(cliplist[.,2], ymin, ymax, yminr, ymaxr)
cliplist[.,3] = rescale2(cliplist[.,3], xmin, xmax, xminr, xmaxr)
cliplist[.,4] = rescale2(cliplist[.,4], ymin, ymax, yminr, ymaxr)
vorl[.,1] = rescale2(vorl[.,1], xmin, xmax, xminr, xmaxr)
vorl[.,2] = rescale2(vorl[.,2], ymin, ymax, yminr, ymaxr)
vorl[.,3] = rescale2(vorl[.,3], xmin, xmax, xminr, xmaxr)
vorl[.,4] = rescale2(vorl[.,4], ymin, ymax, yminr, ymaxr)


// export the voronoi lines

st_matrix("vorline", cliplist)
vorl = cliplist
st_matrix("vorlines", vorl)


////////////////////////////////
Expand All @@ -279,8 +279,8 @@ function voronoi_core(triangles, points, points2, halfedges, hull, offs, vorl, v


// interior points
point0c = cliplist[1::rows(voredges),1..2]
point1c = cliplist[1::rows(voredges),3..4]
point0c = vorl[1::rows(voredges),1..2]
point1c = vorl[1::rows(voredges),3..4]


vorpoly = J(rows(voredges) * 4, 4, .)
Expand Down Expand Up @@ -358,16 +358,14 @@ function voronoi_core(triangles, points, points2, halfedges, hull, offs, vorl, v
vorpoly2 = vorpoly2, J(rows(vorpoly2),1,1)


vorpolyall = vorpoly \ vorpoly2
vorpolyall = uniqrows(vorpolyall)
vorpolyall = vorpolyall, J(rows(vorpolyall),1,.)
vorpolyall = select(vorpolyall, (vorpolyall[.,2] :< .)) // drop the missing rows
vorp = vorpoly \ vorpoly2
vorp = uniqrows(vorp)
vorp = vorp, J(rows(vorp),1,.)
vorp = select(vorp, (vorp[.,2] :< .)) // drop the missing rows


// export the voronoi polygons

mata: st_matrix("vorpolyall", vorpolyall)
vorp = vorpolyall
// st_matrix("vorpolygons", vorp) // moved to export polygons program
}

end
Expand Down Expand Up @@ -963,16 +961,16 @@ program define vorpoly, sortpreserve
drop order
order vpoly_id vpoly_x vpoly_y

mkmat vpoly*, mat(vpoly)
mat colnames vpoly = "vpoly_id" "vpoly_x" "vpoly_y"
mkmat vpoly*, mat(vpolygons)
mat colnames vpolygons = "vpoly_id" "vpoly_x" "vpoly_y"

restore
}


qui {
cap drop vpoly* // make sure the variables are clear
svmat vpoly, n(col)
cap drop vpolygons* // make sure the variables are clear
svmat vpolygons, n(col)

lab var vpoly_id "Voronoi poly: _id"
lab var vpoly_x "Voronoi poly: x"
Expand Down

0 comments on commit 53874e8

Please sign in to comment.