Program: 175834
Project: 175834
Buggy source:
protected void mapTo2d(Face f,Loop<LPoint3d> ll,MiniFacade mf,WallTag wallTag,Set<QuadF> features,MatMeshBuilder faceMaterial,Line megafacade){
  Matrix4d to2dXY=new Matrix4d();
  Vector3d up=f.edge.uphill, along=f.edge.direction(), out=f.edge.getPlaneNormal();
  along.normalize();
  to2dXY.setRow(2,up.x,up.y,up.z,0);
  to2dXY.setRow(1,out.x,out.y,out.z,0);
  to2dXY.setRow(0,-along.x,-along.y,-along.z,0);
  Point3d bottomS=f.definingSE.iterator().next().getStart(f), bottomE=f.definingSE.iterator().next().getEnd(f);
  if (bottomS == null || bottomE == null)   return;
  Point3d start=new Point3d(bottomS);
  Point3d end=new Point3d(bottomE);
  to2dXY.m33=1;
  to2dXY.transform(start);
  to2dXY.m03=-start.x;
  to2dXY.m13=-start.y;
  to2dXY.m23=-start.z;
  start=new Point3d(bottomS);
  to2dXY.transform(start);
  to2dXY.transform(end);
  Loop<LPoint2d> flat=GreebleHelper.to2dLoop(GreebleHelper.transform(ll,to2dXY),1);
  Matrix4d to3d=new Matrix4d(to2dXY);
  to3d.invert();
{
    double[] one=new double[4], two=new double[4];
    to3d.getRow(1,one);
    to3d.getRow(2,two);
    to3d.setRow(1,two);
    to3d.setRow(2,one);
  }
  Matrix4d to2d=new Matrix4d(to3d);
  to2d.invert();
  MiniFacade toRecess=null;
  if (mf != null) {
    toRecess=new MiniFacade(mf);
    toRecess.featureGen=new FeatureGenerator(toRecess);
  }
  LinearForm3D facePlane=new LinearForm3D(new Vector3d(out.x,out.z,out.y),new Point3d(bottomS.x,bottomS.z,bottomS.y));
  LoopL<Point2d> sides=null;
  DRectangle facadeRect=null;
  if (wallTag != null) {
    sides=GreebleHelper.findRectagle(flat,Pointz.to2XZ(start),Pointz.to2XZ(end));
    if (mf != null) {
      if (sides != null)       facadeRect=GreebleHelper.findRect(sides.remove(0));
    }
  }
  DRectangle uvs;
  if (mf != null && mf.app.textureUVs == TextureUVs.SQUARE) {
    uvs=new DRectangle(mf.postState.outerFacadeRect);
{
      Face f2=f;
      while (f2.parent != null)       f2=f2.parent;
      Point3d bottomS2=new Point3d(f2.definingSE.iterator().next().getStart(f2));
      to2dXY.transform(bottomS2);
      uvs.x+=bottomS2.x;
      uvs.y-=bottomS.z;
    }
  }
 else   uvs=GreebleGrid.ZERO_ONE_UVS;
  List<DRectangle> floors=new ArrayList();
  List<MatMeshBuilder> materials=new ArrayList();
  if (wallTag != null && facadeRect != null && mf != null && wallTag.isGroundFloor && mf.groundFloorHeight > 0 && wallTag.groundFloorColor != null && facadeRect.x < mf.groundFloorHeight && facadeRect.getMaxX() > mf.groundFloorHeight) {
    floors.addAll(facadeRect.splitY(mf.groundFloorHeight));
    MatMeshBuilder gfm=greebleGrid.mbs.get(BRICK,wallTag.groundFloorColor);
    for (    Loop<Point2d> loop : sides) {
      Loop<Point2d>[] cut=Loopz.cutConvex(loop,new LinearForm(0,1,mf.groundFloorHeight));
      faceMaterial.add(cut[1].singleton(),to3d);
      LoopL<Point2d> pts=cut[0].singleton();
      gfm.add(pts,GreebleHelper.wallUVs(pts,mf.postState.outerFacadeRect),to3d);
    }
    materials.add(gfm);
    materials.add(faceMaterial);
  }
 else {
    floors.add(facadeRect);
    materials.add(faceMaterial);
    if (sides != null)     faceMaterial.add(sides,GreebleHelper.wallUVs(sides,uvs),to3d);
  }
  for (int j=0; j < floors.size(); j++) {
    DRectangle floorRect=floors.get(j);
    MatMeshBuilder m=materials.get(j);
    Iterator<QuadF> quit=features.iterator();
    while (quit.hasNext()) {
      QuadF n=quit.next();
      if (n.project(to2d,to3d,flat,facePlane,new Vector3d(along.y,0,-along.x)) && wallTag != null && floorRect != null && toRecess != null) {
        FRect bounds=new FRect(n.original);
        n.setBounds(to2d,bounds);
        if (floorRect.contains(bounds)) {
          toRecess.featureGen.put(n.original.f,bounds);
          quit.remove();
        }
      }
    }
    if (wallTag == null || toRecess == null || floorRect == null) {
      LoopL<LPoint2d> loop=flat.singleton();
      LoopL<Point2d> roofUVs;
      RoofTexApp ra=(RoofTexApp)HasApp.get(roofApp);
switch (ra.appMode) {
default :
        roofUVs=null;
      break;
case Net:
    if (ra.textureUVs != TextureUVs.Rectangle)     roofUVs=GreebleHelper.roofPitchUVs(loop,Pointz.to2XZ(start),Pointz.to2XZ(end),TILE_UV_SCALE);
 else     roofUVs=GreebleHelper.wholeRoofUVs(ll.singleton(),ra.textureRect);
  break;
}
m.add(loop,roofUVs,to3d);
return;
}
if (mf.app.appMode == AppMode.Off) greebleGrid.buildGrid(floorRect,to3d,toRecess,m,wallTag);
 else {
greebleGrid.textureGrid(floorRect,uvs,to3d,toRecess);
}
}
}

Fixed version (ground truth):
protected void mapTo2d(Face f,Loop<LPoint3d> ll,MiniFacade mf,WallTag wallTag,Set<QuadF> features,MatMeshBuilder faceMaterial,Line megafacade){
  Matrix4d to2dXY=new Matrix4d();
  Vector3d up=f.edge.uphill, along=f.edge.direction(), out=f.edge.getPlaneNormal();
  along.normalize();
  to2dXY.setRow(2,up.x,up.y,up.z,0);
  to2dXY.setRow(1,out.x,out.y,out.z,0);
  to2dXY.setRow(0,-along.x,-along.y,-along.z,0);
  Point3d bottomS=f.definingSE.iterator().next().getStart(f), bottomE=f.definingSE.iterator().next().getEnd(f);
  if (bottomS == null || bottomE == null)   return;
  Point3d start=new Point3d(bottomS);
  Point3d end=new Point3d(bottomE);
  to2dXY.m33=1;
  to2dXY.transform(start);
  to2dXY.m03=-start.x;
  to2dXY.m13=-start.y;
  to2dXY.m23=-start.z;
  start=new Point3d(bottomS);
  to2dXY.transform(start);
  to2dXY.transform(end);
  Loop<LPoint2d> flat=GreebleHelper.to2dLoop(GreebleHelper.transform(ll,to2dXY),1);
  Matrix4d to3d=new Matrix4d(to2dXY);
  to3d.invert();
{
    double[] one=new double[4], two=new double[4];
    to3d.getRow(1,one);
    to3d.getRow(2,two);
    to3d.setRow(1,two);
    to3d.setRow(2,one);
  }
  Matrix4d to2d=new Matrix4d(to3d);
  to2d.invert();
  MiniFacade toRecess=null;
  if (mf != null) {
    toRecess=new MiniFacade(mf);
    toRecess.featureGen=new FeatureGenerator(toRecess);
  }
  LinearForm3D facePlane=new LinearForm3D(new Vector3d(out.x,out.z,out.y),new Point3d(bottomS.x,bottomS.z,bottomS.y));
  LoopL<Point2d> sides=null;
  DRectangle facadeRect=null;
  if (wallTag != null) {
    sides=GreebleHelper.findRectagle(flat,Pointz.to2XZ(start),Pointz.to2XZ(end));
    if (mf != null) {
      if (sides != null)       facadeRect=GreebleHelper.findRect(sides.remove(0));
    }
  }
  DRectangle uvs;
  if (mf != null && mf.app.textureUVs == TextureUVs.SQUARE) {
    uvs=new DRectangle(mf.postState.outerFacadeRect);
{
      Face f2=f;
      while (f2.parent != null)       f2=f2.parent;
      Point3d bottomS2=new Point3d(f2.definingSE.iterator().next().getStart(f2));
      to2dXY.transform(bottomS2);
      uvs.x+=bottomS2.x;
      uvs.y-=bottomS.z;
    }
  }
 else   uvs=GreebleGrid.ZERO_ONE_UVS;
  List<DRectangle> floors=new ArrayList();
  List<MatMeshBuilder> materials=new ArrayList();
  if (wallTag != null && facadeRect != null && mf != null && wallTag.isGroundFloor && mf.groundFloorHeight > 0 && wallTag.groundFloorColor != null && facadeRect.x < mf.groundFloorHeight && facadeRect.getMaxX() > mf.groundFloorHeight) {
    floors.addAll(facadeRect.splitY(mf.groundFloorHeight));
    MatMeshBuilder gfm=greebleGrid.mbs.get(BRICK,wallTag.groundFloorColor);
    for (    Loop<Point2d> loop : sides) {
      Loop<Point2d>[] cut=Loopz.cutConvex(loop,new LinearForm(0,1,mf.groundFloorHeight));
      faceMaterial.add(cut[1].singleton(),to3d);
      LoopL<Point2d> pts=cut[0].singleton();
      gfm.add(pts,GreebleHelper.wallUVs(pts,mf.postState.outerFacadeRect),to3d);
    }
    materials.add(gfm);
    materials.add(faceMaterial);
  }
 else {
    floors.add(facadeRect);
    materials.add(faceMaterial);
    if (sides != null)     faceMaterial.add(sides,GreebleHelper.wallUVs(sides,uvs),to3d);
  }
  for (int j=0; j < floors.size(); j++) {
    DRectangle floorRect=floors.get(j);
    MatMeshBuilder m=materials.get(j);
    Iterator<QuadF> quit=features.iterator();
    while (quit.hasNext()) {
      QuadF n=quit.next();
      if (n.project(to2d,to3d,flat,facePlane,new Vector3d(along.y,0,-along.x)) && wallTag != null && floorRect != null && toRecess != null) {
        FRect bounds=new FRect(n.original);
        n.setBounds(to2d,bounds);
        if (floorRect.contains(bounds)) {
          toRecess.featureGen.put(n.original.f,bounds);
          quit.remove();
        }
      }
    }
    if (wallTag == null || toRecess == null || floorRect == null) {
      LoopL<LPoint2d> loop=flat.singleton();
      LoopL<Point2d> roofUVs;
      RoofTexApp ra=(RoofTexApp)HasApp.get(roofApp);
switch (ra.appMode) {
default :
        roofUVs=null;
      break;
case Net:
    if (ra.textureUVs != TextureUVs.Rectangle)     roofUVs=GreebleHelper.roofPitchUVs(loop,Pointz.to2XZ(start),Pointz.to2XZ(end),TILE_UV_SCALE);
 else     roofUVs=GreebleHelper.wholeRoofUVs(ll.singleton(),ra.textureRect);
  break;
}
m.add(loop,roofUVs,to3d);
return;
}
if (mf.app.appMode == AppMode.Off || mf.app.texture == null) greebleGrid.buildGrid(floorRect,to3d,toRecess,m,wallTag);
 else {
greebleGrid.textureGrid(floorRect,uvs,to3d,toRecess);
}
}
}