Files
Hi.Sample/Disp/DemoDrawing.cs
T
iambossandClaude Opus 5 40214ca6f4 feat(demos): take over the ten framework-independent samples from Hi.Sample.Wpf
Hi.WinForm and Hi.WpfPlus are being retired, and Hi.Sample.Wpf existed only to
demonstrate Hi.WpfPlus -- but ten of its fourteen files never touched WPF at all. Those
move here rather than leaving with it:

- The three displayees upgrading-to-3.2/breaking-changes.md tells callers to copy --
  MachiningProjectDisplayee, IsoCoordinateEntryDisplayee,
  HeidenhainCoordinateEntryDisplayee. They compose only public API over
  LocalProjectService, which is the point of shipping them as a starting sample; that doc
  now points at Hi.Sample/Disp/.
- Seven display demos -- Cylindroid, DiscreteRgb, Drawing, Pick1, Pick2, Pickable,
  Satellite. Their only WPF coupling was the closing DemoUtil.RunApplication call.

DemoUtil is rewritten for a project with no canvas. HiAPI ships none any more, so a
sample cannot end by opening a window; Present() initializes the app, reports the
displayee's type and bounding box, and shuts down, which makes the samples runnable and
checkable headlessly and puts the handover where it actually is -- the application
assigns the composed IDisplayee to its own canvas' DispEngine.

The four files that genuinely need WPF (DemoStl, the old DemoUtil, and the two Machining
demos) stay behind and retire with Hi.Sample.Wpf. Each moved file's own
[!code-csharp] doc-comment path was repointed from ~/../Hi.Sample.Wpf/Disp/ to
~/../Hi.Sample/Disp/. Builds clean at x64: 0 warnings, 0 errors.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-21 11:48:33 +08:00

202 lines
5.8 KiB
C#

using Hi.Geom;
using Hi.Disp;
using System.Collections.Generic;
using Hi.Disp.Treat;
using Hi.Coloring;
namespace Sample.Disp
{
/// <summary>
/// Demonstrates various drawing techniques using the HiAPI graphics system.
/// Provides examples of primitive drawing, attribute specification, and rendering options.
/// Includes methods for drawing lines, points, triangles, and other geometric primitives.
/// </summary>
/// <remarks>
/// ### Source Code
/// [!code-csharp[SampleCode](~/../Hi.Sample/Disp/DemoDrawing.cs)]
/// </remarks>
public static class DemoDrawing
{
/// <summary>
/// Demonstrates simple line drawing using raw vertex coordinates.
/// Creates a basic line strip from three vertices and displays it using DemoUtil.
/// </summary>
public static void FreeDrawing()
{
#region DocSite.FreeDrawing
double[] vs =
{0,0,0
,1,0,0
,0,0,1};
DemoUtil.Present("EasyDraw"
, new Drawing(vs, Stamp.V, GL.GL_LINE_STRIP));
#endregion
}
/// <summary>
/// Demonstrates various types of line drawing techniques.
/// Shows how to create points, line strips, line strips with normals, and colored line strips.
/// Illustrates different ways to specify vertex attributes like position, color, and normal.
/// </summary>
public static void DrawLines()
{
List<Vec3d> ps;
double[] vs;
int n = 10; //vertex num
////create drawPoints
ps = new List<Vec3d>(n);
for (int i = 0; i < n; i++)
ps.Add(new Vec3d(i, 0, 0));
vs = new double[ps.Count * 3];
for (int i = 0; i < ps.Count; i++)
{
vs[3 * i] = ps[i].x;
vs[3 * i + 1] = ps[i].y;
vs[3 * i + 2] = ps[i].z;
}
//V means Vertex
Drawing drawPoints = new Drawing(vs, Stamp.V, GL.GL_POINTS);
////create drawLineStrip
vs = new double[ps.Count * 3];
for (int i = 0; i < ps.Count; i++)
{
vs[3 * i] = i;
vs[3 * i + 1] = 0;
vs[3 * i + 2] = 1;
}
Drawing drawLineStrip = new Drawing(vs, Stamp.V, GL.GL_LINE_STRIP);
////create drawLineStripWithNormal
//the content of vs is Nx,Ny,Nz,Vx,Vy,Vz, Nx,Ny,Nz,Vx,Vy,Vz, ...
vs = new double[n * 6];
for (int i = 0; i < n; i++)
{
//nx,ny,nz
vs[6 * i] = 1;
vs[6 * i + 1] = 0;
vs[6 * i + 2] = 0;
//x,y,z
vs[6 * i + 3] = i;
vs[6 * i + 4] = 0;
vs[6 * i + 5] = 2;
}
//V means Vertex; N means Normal
Drawing drawLineStripWithNormal =
new Drawing(vs, Stamp.NV, GL.GL_LINE_STRIP);
////create drawColorLines
vs = new double[n * 6];
for (int i = 0; i < n; i++)
{
//the rgb values set casually.
//r,g,b
vs[6 * i] = i % 3 / 2D;
vs[6 * i + 1] = i % 5 / 4D;
vs[6 * i + 2] = i % 7 / 6D;
//x,y,z
vs[6 * i + 3] = i;
vs[6 * i + 4] = 0;
vs[6 * i + 5] = 3;
}
//V means Vertex; C means Color
Drawing drawLineStripWithColor = new Drawing(vs, Stamp.CV, GL.GL_LINE_STRIP);
DemoUtil.Present("DrawLines",
new DispList(drawPoints, drawLineStrip
, drawLineStripWithNormal, drawLineStripWithColor));
}
/// <summary>
/// Demonstrates drawing triangles using OpenGL primitives.
/// Shows how to create colored triangles (CV) and colored triangles with normals (CNV).
/// Illustrates packing of vertex attributes (color, normal, position) into arrays for rendering.
/// </summary>
public static void DrawTrianglesByPrimitives()
{
int n = 10;// triangle num
double[] vs;
////create drawTrisCV
//each triangle has 3 point; each point has r,g,b,x,y,z, totally 6 doubles.
vs = new double[n * 3 * 6];
for (int i = 0, k = 0; i < n; i++)
{
//the rgb values set as casual.
Vec3d rgb = ColorUtil.GetDiscreteRgb(i);
Tri3d tri = new Tri3d(new Vec3d(i, 0, 0), new Vec3d(i + 1, 0, 0), new Vec3d(i + 0.5, 0, 1));
for (int j = 0; j < 3; j++)
{
vs[k++] = rgb.x;
vs[k++] = rgb.y;
vs[k++] = rgb.z;
vs[k++] = tri.ps[j].x;
vs[k++] = tri.ps[j].y;
vs[k++] = tri.ps[j].z;
}
}
//V means Vertex; C means Color
Drawing drawTrisCV = new Drawing(vs, Stamp.CV, GL.GL_TRIANGLES);
////create drawTrisCNV
/////each triangle has 3 point; each point has r,g,b,nx,ny,nz,x,y,z, totally 6 doubles.
vs = new double[n * 3 * 9];
for (int i = 0, k = 0; i < n; i++)
{
//the rgb values set as casual.
Vec3d rgb = ColorUtil.GetDiscreteRgb(i);
Tri3d tri = new Tri3d(new Vec3d(i, 0, 3), new Vec3d(i + 1, 0, 3), new Vec3d(i + 0.5, 0, 4));
Vec3d nn = tri.GetNormal();
for (int j = 0; j < 3; j++)
{
vs[k++] = rgb.x;
vs[k++] = rgb.y;
vs[k++] = rgb.z;
vs[k++] = nn.x;
vs[k++] = nn.y;
vs[k++] = nn.z;
vs[k++] = tri.ps[j].x;
vs[k++] = tri.ps[j].y;
vs[k++] = tri.ps[j].z;
}
}
//V means Vertex; C means Color; N means Normal
Drawing drawTrisCNV = new Drawing(vs, Stamp.CNV, GL.GL_TRIANGLES);
//the color of colorTrisDraw is not influence by outer color setting.
DemoUtil.Present("DrawTrianglesByPrimitives",
new DispList(new RgbTreat(1, 0, 0), drawTrisCV, drawTrisCNV));
}
/// <summary>
/// Demonstrates drawing 3D triangles using the Tri3d helper class.
/// Creates a series of triangles and renders them as both face (filled) and line (wireframe) drawings.
/// Shows how to apply different colors to faces and lines of the same geometry.
/// </summary>
public static void DrawTri3d()
{
int n = 10;
List<Tri3d> tris = new List<Tri3d>(n);
for (int i = 0; i < n; i++)
tris.Add(new Tri3d(new Vec3d(i, 0, 0), new Vec3d(i + 1, 0, 0), new Vec3d(i + 0.5, 0, 1)));
Drawing faceDraw = tris.GetFaceDrawing();
Drawing linesDraw = tris.ToLineDrawing();
DemoUtil.Present("DrawTri3d",
new DispList(new RgbTreat(0, 0, 1), linesDraw
, new RgbTreat(1, 0, 0), faceDraw));
}
static void Main()
{
//FreeDrawing();
//DrawLines();
//DrawTrianglesByPrimitives();
DrawTri3d();
}
}
}