Skip to content

Commit

Permalink
Merge pull request #84 from imbasimba/catalog-hips
Browse files Browse the repository at this point in the history
Catalog HiPS
  • Loading branch information
pkgw authored Mar 18, 2021
2 parents 29a47b8 + 2e0a8a3 commit c9000e9
Show file tree
Hide file tree
Showing 14 changed files with 826 additions and 198 deletions.
273 changes: 228 additions & 45 deletions engine/wwtlib/HealpixTile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public class HealpixTile : Tile
private int step;
private string url;
private bool subDivided = false;
private static Matrix3d galacticMatrix = Matrix3d.Create(
private readonly List<List<string>> catalogRows = new List<List<string>>();
private WebFile catalogData;
private static readonly Matrix3d galacticMatrix = Matrix3d.Create(
-0.0548755604024359, -0.4838350155267381, -0.873437090247923, 0,
-0.8676661489811610, 0.4559837762325372, -0.1980763734646737, 0,
0.4941094279435681, 0.7469822444763707, -0.4448296299195045, 0,
Expand Down Expand Up @@ -75,7 +77,8 @@ public HealpixTile(int level, int x, int y, Imageset dataset, Tile parent)
this.faceY = parentTile.faceY * 2 + y;
}


IsCatalogTile = dataset.HipsProperties.Properties.ContainsKey("dataproduct_type")
&& dataset.HipsProperties.Properties["dataproduct_type"].ToLowerCase() == "catalog";
// All healpix is inside out
//insideOut = true;
ComputeBoundingSphere();
Expand Down Expand Up @@ -224,29 +227,24 @@ private string GetHipsFileExtention()
//prioritize transparent Png over other image formats
if (dataset.Extension.ToLowerCase().IndexOf("png") > -1)
{
IsCatalogTile = false;
return ".png";
}

// Check for either type
if (dataset.Extension.ToLowerCase().IndexOf("jpeg") > -1 || dataset.Extension.ToLowerCase().IndexOf("jpg") > -1)
{
IsCatalogTile = false;
return ".jpg";
}

if (dataset.Extension.ToLowerCase().IndexOf("tsv") > -1)
{
IsCatalogTile = true;
return ".tsv";
}

if (dataset.Extension.ToLowerCase().IndexOf("fits") > -1)
{
IsCatalogTile = false;
return ".fits";
}
IsCatalogTile = false;

//default to most common
return ".jpg";
Expand Down Expand Up @@ -292,6 +290,11 @@ private void SetCorners()

public override bool Draw3D(RenderContext renderContext, double opacity)
{
if (IsCatalogTile)
{
DrawCatalogTile(renderContext, opacity);
return true;
}
RenderedGeneration = CurrentRenderGeneration;
TilesTouched++;

Expand Down Expand Up @@ -362,15 +365,6 @@ public override bool Draw3D(RenderContext renderContext, double opacity)
{
renderChildPart[childIndex].TargetState = renderChildPart[childIndex].State = false;
}

//if (renderChildPart[childIndex].TargetState == true || !blendMode)
//{
// renderChildPart[childIndex].State = renderChildPart[childIndex].TargetState;
//}
//if (renderChildPart[childIndex].TargetState != renderChildPart[childIndex].State)
//{
// transitioning = true;
//}
}
else
{
Expand Down Expand Up @@ -400,7 +394,6 @@ public override bool Draw3D(RenderContext renderContext, double opacity)
Parent.RenderedAtOrBelowGeneration = RenderedAtOrBelowGeneration;
}
}

if (!anythingToRender)
{
return true;
Expand All @@ -418,7 +411,6 @@ public override bool Draw3D(RenderContext renderContext, double opacity)

TilesInView++;


for (int i = 0; i < 4; i++)
{
if (renderChildPart[i].TargetState)
Expand All @@ -430,33 +422,226 @@ public override bool Draw3D(RenderContext renderContext, double opacity)
return true;
}

public void DrawCatalogTile(RenderContext renderContext, double opacity)
{
RenderedGeneration = CurrentRenderGeneration;
TilesTouched++;

InViewFrustum = true;
bool onlyDrawChildren = false;

if (!ReadyToRender)
{
if (!errored)
{
TileCache.AddTileToQueue(this);
return;
}

if (errored && Level < 3) //Level 0-2 sometimes deleted in favor of allsky.jpg/tsv
{
onlyDrawChildren = true;
}
else
{
return;
}
}

bool anyChildInFrustum = false;
int childIndex = 0;
for (int y1 = 0; y1 < 2; y1++)
{
for (int x1 = 0; x1 < 2; x1++)
{
if (Level < dataset.Levels)
{
if (children[childIndex] == null)
{
children[childIndex] = TileCache.GetTile(Level + 1, x1, y1, dataset, this);
}

if (children[childIndex].IsTileInFrustum(renderContext.Frustum))
{
InViewFrustum = true;
anyChildInFrustum = true;
if (children[childIndex].IsTileBigEnough(renderContext) || onlyDrawChildren)
{
((HealpixTile)children[childIndex]).DrawCatalogTile(renderContext, opacity);
}
else
{
((HealpixTile)children[childIndex]).RemoveCatalogTile();
}
}
else
{
((HealpixTile)children[childIndex]).RemoveCatalogTile();
}
}

childIndex++;
}
}
if (Level == 0 && !anyChildInFrustum && !onlyDrawChildren)
{
RemoveCatalogTile();
} else if (anyChildInFrustum)
{
TilesInView++;
AddCatalogTile();
}
}

public void RemoveCatalogTile()
{
dataset.HipsProperties.CatalogSpreadSheetLayer.RemoveTileRows(Key, catalogRows);
}

private void AddCatalogTile()
{
dataset.HipsProperties.CatalogSpreadSheetLayer.AddTileRows(Key, catalogRows);
}

private void ExtractCatalogTileRows()
{
bool headerRemoved = false;
foreach (string line in catalogData.GetText().Split("\n"))
{
if (!line.StartsWith("#") && !headerRemoved)
{
headerRemoved = true;
continue;
}

if (!line.StartsWith("#"))
{
List<string> rowData = UiTools.SplitString(line, dataset.HipsProperties.CatalogSpreadSheetLayer.Table.Delimiter);
catalogRows.Add(rowData);
}
}
}


public bool GetDataInView(RenderContext renderContext, bool limit, CatalogSpreadSheetLayer catalogSpreadSheetLayer)
{
if (!ReadyToRender)
{
if (!errored)
{
RequestImage();
if (limit)
{
return false;
}
} else if(Level >= 3) //Level 0-2 sometimes deleted in favor of allsky.jpg/tsv
{
return true;
}
}

bool allChildrenReady = true;
bool anyChildInFrustum = false;
int childIndex = 0;
for (int y1 = 0; y1 < 2; y1++)
{
for (int x1 = 0; x1 < 2; x1++)
{
if (Level < dataset.Levels)
{
if (children[childIndex] == null)
{
children[childIndex] = TileCache.GetTile(Level + 1, x1, y1, dataset, this);
}

if (children[childIndex].IsTileInFrustum(renderContext.Frustum))
{
anyChildInFrustum = true;
allChildrenReady = allChildrenReady && ((HealpixTile)children[childIndex]).GetDataInView(renderContext, limit, catalogSpreadSheetLayer);
}
}

childIndex++;
}
}
if (anyChildInFrustum)
{
catalogSpreadSheetLayer.AddTileRows(Key, catalogRows);
}
return allChildrenReady && !Downloading;
}

private void SetStep()
{
switch (Level)
{
case 0:
case 1:
case 2:
step = 64;
break;
case 3:
step = 32;
break;
case 4:
step = 16;
break;
case 5:
step = 8;
break;
case 6:
step = 4;
break;
case 7:
step = 2;
break;
default:
step = 2;
break;
if (IsCatalogTile)
{
step = 2;
} else
{
switch (Level)
{
case 0:
case 1:
case 2:
step = 64;
break;
case 3:
step = 32;
break;
case 4:
step = 16;
break;
case 5:
step = 8;
break;
case 6:
step = 4;
break;
default:
step = 2;
break;
}
}
}


public override void RequestImage()
{
if (IsCatalogTile)
{
if (!Downloading && !ReadyToRender)
{
Downloading = true;
catalogData = new WebFile(this.URL);
catalogData.OnStateChange = LoadCatalogData;
catalogData.Send();
}
}
else
{
base.RequestImage();
}

}

private void LoadCatalogData()
{
if (catalogData.State == StateType.Error)
{
RequestPending = false;
Downloading = false;
errored = true;
TileCache.RemoveFromQueue(this.Key, true);
}
else if (catalogData.State == StateType.Received)
{
ExtractCatalogTileRows();
texReady = true;
Downloading = false;
errored = false;
ReadyToRender = true;
RequestPending = false;
TileCache.RemoveFromQueue(this.Key, true);
}
}

Expand Down Expand Up @@ -529,8 +714,6 @@ public override double GetSurfacePointAltitude(double lat, double lng, bool mete

if (Level < lastDeepestLevel)
{
//interate children

foreach (Tile child in children)
{
if (child != null)
Expand Down
Loading

0 comments on commit c9000e9

Please sign in to comment.