using System;
using System.Collections.Generic;
using Rhino;
using Rhino.Commands;
using Rhino.Geometry;
namespace TestingVisualTech
{
public class Lamp : Command
{
public Lamp()
{
Instance = this;
}
///<summary>The only instance of this command.</summary>
public static Lamp Instance { get; private set; }
public override string EnglishName => "AdvancedTexturedLamp";
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
// --- General Parameters ---
double height = 320;
double baseRadius = 100;
double neckRadius = 80;
// --- Enhanced Profile Parameters ---
double bulgeHeight1 = 0.3; // First bulge position (30% up)
double bulgeHeight2 = 0.7; // Second bulge position (70% up)
double bulgeIntensity = 1.4; // How pronounced the bulges are
// --- Variable Twist Parameters ---
double maxTwistAmount = 2 * Math.PI * 0.15; // Maximum twist
double twistVariation = 2.0; // How twist varies along height
// --- Multi-frequency Wave Parameters ---
double primaryWaveDepth = 0.08;
int primaryWaveCount = 12;
double secondaryWaveDepth = 0.04;
int secondaryWaveCount = 30;
double tertiaryWaveDepth = 0.02;
int tertiaryWaveCount = 75;
// --- Asymmetric Features ---
double asymmetryAmount = 0.03;
double asymmetryFreq = 3.0;
// --- Organic Variation ---
double organicVariation = 0.02;
// --- Resolution ---
int sectionCount = 60;
int pointsPerSection = 64;
var sections = new List<Curve>();
Random rand = new Random(42); // Fixed seed for reproducible results
for (int i = 0; i < sectionCount; i++)
{
double t = (double)i / (sectionCount - 1);
double z = t * height;
// Enhanced profile with multiple bulges and smooth transitions
double profileBase = Math.Sin(Math.PI * t);
// Add bulges at specific heights
double bulge1 = Math.Exp(-Math.Pow((t - bulgeHeight1) * 8, 2)) * bulgeIntensity;
double bulge2 = Math.Exp(-Math.Pow((t - bulgeHeight2) * 6, 2)) * (bulgeIntensity * 0.8);
// Smooth waist in the middle
double waist = 1.0 - 0.3 * Math.Exp(-Math.Pow((t - 0.5) * 4, 2));
double profileFactor = (profileBase + bulge1 + bulge2) * waist;
double radius = baseRadius + (neckRadius - baseRadius) * profileFactor;
// Variable twist that changes along height
double twistFactor = Math.Sin(Math.PI * t * twistVariation) * (1.0 + t);
double twist = maxTwistAmount * t * twistFactor;
var pts = new List<Point3d>();
for (int j = 0; j < pointsPerSection; j++)
{
double angle = 2 * Math.PI * j / pointsPerSection;
double twistedAngle = angle + twist;
// Multi-frequency waves for rich surface texture
double primaryWave = primaryWaveDepth * Math.Sin(primaryWaveCount * twistedAngle);
double secondaryWave = secondaryWaveDepth * Math.Sin(secondaryWaveCount * twistedAngle + Math.PI/3);
double tertiaryWave = tertiaryWaveDepth * Math.Sin(tertiaryWaveCount * twistedAngle + Math.PI/6);
// Asymmetric features
double asymmetry = asymmetryAmount * Math.Sin(asymmetryFreq * twistedAngle) * Math.Cos(2 * Math.PI * t);
// Organic variation using noise-like function
double organic = organicVariation * (rand.NextDouble() - 0.5) *
Math.Sin(7 * twistedAngle + 3 * Math.PI * t);
// Height-dependent modulation
double heightModulation = 1.0 + 0.1 * Math.Sin(4 * Math.PI * t) * Math.Cos(3 * twistedAngle);
double combinedModulation = heightModulation + primaryWave + secondaryWave +
tertiaryWave + asymmetry + organic;
double r = radius * combinedModulation;
// Add slight elliptical distortion for more organic feel
double ellipticalFactor = 1.0 + 0.05 * Math.Sin(2 * twistedAngle + Math.PI * t);
double x = r * Math.Cos(angle) * ellipticalFactor;
double y = r * Math.Sin(angle);
pts.Add(new Point3d(x, y, z));
}
pts.Add(pts[0]); // Close the curve
var curve = Curve.CreateInterpolatedCurve(pts, 3, CurveKnotStyle.Uniform);
if (curve != null && curve.IsValid)
sections.Add(curve);
}
// Create the loft
var loft = Brep.CreateFromLoft(sections, Point3d.Unset, Point3d.Unset, LoftType.Tight, false);
if (loft != null && loft.Length > 0)
{
Brep resultBrep = loft[0];
resultBrep.CapPlanarHoles(doc.ModelAbsoluteTolerance);
doc.Objects.AddBrep(resultBrep);
doc.Views.Redraw();
RhinoApp.WriteLine("Advanced textured lamp with complex geometry generated.");
return Result.Success;
}
RhinoApp.WriteLine("Error in Loft generation.");
return Result.Failure;
}
}
}