/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object can be used to retrieve data from input parameters and /// to store data in output parameters.</param> protected override void SolveInstance(IGH_DataAccess DA) { Component = this; GrasshopperDocument = this.OnPingDocument(); int inputCount = Component.Params.Input.Count; BehaviorData behaviorData = new BehaviorData(); List <ForceData> forceDataList = new List <ForceData>(); List <string> stringlist = new List <string>(); List <string> behaviorNames = new List <string>(); int hitCounter = 0; for (int i = 0; i < inputCount; i++) { IGH_DocumentObject connectedComponent = Component.Params.Input[i].Sources[0].Attributes.GetTopLevel.DocObject; string name = connectedComponent.Name; Component.Params.Input[i].NickName = name; foreach (IGH_Goo a in Component.Params.Input[i].VolatileData.get_Branch(0)) { if (a.ToString() == "Culebra_GH.Data_Structures.FlockingData") { hitCounter++; FlockingData fd; bool worked = a.CastTo(out fd); if (!worked) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "We could not cast to Flocking data structure, please check your inputs"); return; } behaviorData.flockData = fd; behaviorNames.Add("Flocking"); stringlist.Add("Alignment Value = " + fd.alignment_Value.ToString()); stringlist.Add("Separation Value = " + fd.separation_Value.ToString()); } else if (a.ToString() == "Culebra_GH.Data_Structures.WanderingData") { hitCounter++; WanderingData wd; bool worked = a.CastTo(out wd); if (!worked) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "We could not cast to Wandering data structure, please check your inputs"); return; } behaviorData.wanderData = wd; behaviorNames.Add("Wandering"); stringlist.Add("Wandering Radius Value = " + wd.wanderingRadius.ToString()); stringlist.Add("Wandering Distance Value = " + wd.wanderingDistance.ToString()); } else if (a.ToString() == "Culebra_GH.Data_Structures.TrackingData") { hitCounter++; TrackingData td; bool worked = a.CastTo(out td); if (!worked) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "We could not cast to Tracking data structure, please check your inputs"); return; } behaviorData.trackingData = td; behaviorNames.Add("Tracking"); } else if (a.ToString() == "Culebra_GH.Data_Structures.StigmergyData") { hitCounter++; StigmergyData sd; bool worked = a.CastTo(out sd); if (!worked) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "We could not cast to Stigmergy data structure, please check your inputs"); return; } behaviorData.stigmergyData = sd; behaviorNames.Add("Stigmergy"); } else if (a.ToString() == "Culebra_GH.Data_Structures.NoiseData") { hitCounter++; NoiseData nd; bool worked = a.CastTo(out nd); if (!worked) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "We could not cast to Noise data structure, please check your inputs"); return; } behaviorData.noiseData = nd; behaviorNames.Add("Noise"); } else if (a.ToString() == "Culebra_GH.Data_Structures.ForceData") { hitCounter++; ForceData data; bool worked = a.CastTo(out data); if (!worked) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "We could not cast to Forces data structure, please check your inputs"); return; } forceDataList.Add(data); behaviorData.forceData = forceDataList; behaviorNames.Add("Force"); } else if (a.ToString() == "Culebra_GH.Data_Structures.SeparationData") { hitCounter++; SeparationData data; bool worked = a.CastTo(out data); if (!worked) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "We could not cast to Separation data structure, please check your inputs"); return; } behaviorData.separationData = data; behaviorNames.Add("Separation"); } else if (a.ToString() == "Culebra_GH.Data_Structures.MeshCrawlData") { hitCounter++; MeshCrawlData data; bool worked = a.CastTo(out data); if (!worked) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "We could not cast to Mesh Crawl data structure, please check your inputs"); return; } behaviorData.meshCrawlData = data; behaviorNames.Add("Crawl"); } else if (a.ToString() == "Culebra_GH.Data_Structures.BundlingData") { hitCounter++; BundlingData data; bool worked = a.CastTo(out data); if (!worked) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "We could not cast to Bundling data structure, please check your inputs"); return; } behaviorData.bundlingData = data; behaviorNames.Add("Bundling"); } else { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Could not convert incoming data"); return; } } } if (hitCounter > 0) { behaviorData.dataOrder = behaviorNames; IGH_BehaviorData igh_Behavior = new IGH_BehaviorData(behaviorData); DA.SetData(0, igh_Behavior); } }
/// <summary> /// Filters through the Controller Data /// </summary> private void FilterBehaviors(Creeper c, List <CulebraObject> creepList, object behavioral_Settings, int dimensions, List <Vector3d> childSpawners, List <int> childSpawnType, List <Vector3d> totTail) { IGH_BehaviorData igh_Behavior = (IGH_BehaviorData)behavioral_Settings; foreach (string s in igh_Behavior.Value.dataOrder) { if (s == "Flocking") { if (dimensions == 0) { if (igh_Behavior.Value.flockData.colorMesh != null) { c.behaviors.Flock2D(igh_Behavior.Value.flockData.searchRadius, igh_Behavior.Value.flockData.cohesion_Value, igh_Behavior.Value.flockData.separation_Value, igh_Behavior.Value.flockData.alignment_Value, igh_Behavior.Value.flockData.viewAngle, creepList, igh_Behavior.Value.flockData.network, igh_Behavior.Value.flockData.mapAlignment, igh_Behavior.Value.flockData.mapSeparation, igh_Behavior.Value.flockData.mapCohesion, igh_Behavior.Value.flockData.colorMesh); } else { c.behaviors.Flock2D(igh_Behavior.Value.flockData.searchRadius, igh_Behavior.Value.flockData.cohesion_Value, igh_Behavior.Value.flockData.separation_Value, igh_Behavior.Value.flockData.alignment_Value, igh_Behavior.Value.flockData.viewAngle, creepList, igh_Behavior.Value.flockData.network); } } else if (dimensions == 1) { if (igh_Behavior.Value.flockData.colorMesh != null) { c.behaviors.Flock3D(igh_Behavior.Value.flockData.searchRadius, igh_Behavior.Value.flockData.cohesion_Value, igh_Behavior.Value.flockData.separation_Value, igh_Behavior.Value.flockData.alignment_Value, igh_Behavior.Value.flockData.viewAngle, creepList, igh_Behavior.Value.flockData.network, igh_Behavior.Value.flockData.mapAlignment, igh_Behavior.Value.flockData.mapSeparation, igh_Behavior.Value.flockData.mapCohesion, igh_Behavior.Value.flockData.colorMesh); } else { c.behaviors.Flock3D(igh_Behavior.Value.flockData.searchRadius, igh_Behavior.Value.flockData.cohesion_Value, igh_Behavior.Value.flockData.separation_Value, igh_Behavior.Value.flockData.alignment_Value, igh_Behavior.Value.flockData.viewAngle, creepList, igh_Behavior.Value.flockData.network); } } } else if (s == "Bundling") { throw new Exception("You have input bundling behavior, Creepers engine cannot run bundling behavior, please use Bundling Engine"); } else if (s == "Wandering") { if (dimensions == 0) { if (igh_Behavior.Value.wanderData.wanderingType == "Wander") { if (igh_Behavior.Value.wanderData.colorMesh != null) { c.behaviors.Wander2D(igh_Behavior.Value.wanderData.randomize, igh_Behavior.Value.wanderData.addHeading, igh_Behavior.Value.wanderData.change, igh_Behavior.Value.wanderData.wanderingRadius, igh_Behavior.Value.wanderData.wanderingDistance, igh_Behavior.Value.wanderData.mapChange, igh_Behavior.Value.wanderData.mapRadius, igh_Behavior.Value.wanderData.mapDistance, igh_Behavior.Value.wanderData.colorMesh); } else { c.behaviors.Wander2D(igh_Behavior.Value.wanderData.randomize, igh_Behavior.Value.wanderData.addHeading, igh_Behavior.Value.wanderData.change, igh_Behavior.Value.wanderData.wanderingRadius, igh_Behavior.Value.wanderData.wanderingDistance); } } else { c.behaviors.SuperWander2D(igh_Behavior.Value.wanderData.change, igh_Behavior.Value.wanderData.wanderingRadius, igh_Behavior.Value.wanderData.wanderingDistance, igh_Behavior.Value.wanderData.rotationTrigger); } } else if (dimensions == 1) { if (igh_Behavior.Value.wanderData.wanderingType == "SuperWander_B") { c.behaviors.Wander3D_subA(igh_Behavior.Value.wanderData.change, igh_Behavior.Value.wanderData.wanderingRadius, igh_Behavior.Value.wanderData.wanderingDistance, igh_Behavior.Value.wanderData.rotationTrigger); } else if (igh_Behavior.Value.wanderData.wanderingType == "SuperWander_C") { c.behaviors.Wander3D_subB(igh_Behavior.Value.wanderData.change, igh_Behavior.Value.wanderData.wanderingRadius, igh_Behavior.Value.wanderData.wanderingDistance, igh_Behavior.Value.wanderData.rotationTrigger); } else { if (igh_Behavior.Value.wanderData.colorMesh != null) { c.behaviors.Wander3D(igh_Behavior.Value.wanderData.change, igh_Behavior.Value.wanderData.wanderingRadius, igh_Behavior.Value.wanderData.wanderingDistance, igh_Behavior.Value.wanderData.rotationTrigger, igh_Behavior.Value.wanderData.mapChange, igh_Behavior.Value.wanderData.mapRadius, igh_Behavior.Value.wanderData.mapDistance, false, igh_Behavior.Value.wanderData.colorMesh); } else { c.behaviors.Wander3D(igh_Behavior.Value.wanderData.change, igh_Behavior.Value.wanderData.wanderingRadius, igh_Behavior.Value.wanderData.wanderingDistance, igh_Behavior.Value.wanderData.rotationTrigger); } } } } else if (s == "Tracking") { if (igh_Behavior.Value.trackingData.triggerBabies) { if (igh_Behavior.Value.trackingData.colorMesh != null) { c.behaviors.MultiPolylineTrackerBabyMaker(igh_Behavior.Value.trackingData.polylines, igh_Behavior.Value.trackingData.pathThreshold, igh_Behavior.Value.trackingData.projectionDistance, igh_Behavior.Value.trackingData.pathRadius, igh_Behavior.Value.trackingData.triggerBabies, igh_Behavior.Value.trackingData.maxChildren, true, childSpawners, childSpawnType, igh_Behavior.Value.trackingData.mapThreshold, igh_Behavior.Value.trackingData.mapProjection, igh_Behavior.Value.trackingData.mapRadius, igh_Behavior.Value.trackingData.colorMesh); this.childSpawners = c.behaviors.GetChildStartPositions(); this.childSpawnType = c.behaviors.GetChildSpawnTypes(); } else { c.behaviors.MultiPolylineTrackerBabyMaker(igh_Behavior.Value.trackingData.polylines, igh_Behavior.Value.trackingData.pathThreshold, igh_Behavior.Value.trackingData.projectionDistance, igh_Behavior.Value.trackingData.pathRadius, igh_Behavior.Value.trackingData.triggerBabies, igh_Behavior.Value.trackingData.maxChildren, true, childSpawners, childSpawnType); this.childSpawners = c.behaviors.GetChildStartPositions(); this.childSpawnType = c.behaviors.GetChildSpawnTypes(); } } else { if (igh_Behavior.Value.trackingData.colorMesh != null) { c.behaviors.MultiPolylineTracker(igh_Behavior.Value.trackingData.polylines, igh_Behavior.Value.trackingData.pathThreshold, igh_Behavior.Value.trackingData.projectionDistance, igh_Behavior.Value.trackingData.pathRadius, igh_Behavior.Value.trackingData.mapThreshold, igh_Behavior.Value.trackingData.mapProjection, igh_Behavior.Value.trackingData.mapRadius, igh_Behavior.Value.trackingData.colorMesh); } else { c.behaviors.MultiPolylineTracker(igh_Behavior.Value.trackingData.polylines, igh_Behavior.Value.trackingData.pathThreshold, igh_Behavior.Value.trackingData.projectionDistance, igh_Behavior.Value.trackingData.pathRadius); } } } else if (s == "Crawl") { if (igh_Behavior.Value.meshCrawlData.triggerBabies) { c.behaviors.MeshWalk(igh_Behavior.Value.meshCrawlData.mesh, igh_Behavior.Value.meshCrawlData.meshThreshold, igh_Behavior.Value.meshCrawlData.meshProjectionDistance, igh_Behavior.Value.meshCrawlData.multiplier, igh_Behavior.Value.meshCrawlData.triggerBabies, true, igh_Behavior.Value.meshCrawlData.maxChildren, childSpawners, childSpawnType); this.childSpawners = c.behaviors.GetMeshCrawler().GetChildStartPositions(); this.childSpawnType = c.behaviors.GetMeshCrawler().GetChildSpawnType(); } else { c.behaviors.MeshWalk(igh_Behavior.Value.meshCrawlData.mesh, igh_Behavior.Value.meshCrawlData.meshThreshold, igh_Behavior.Value.meshCrawlData.meshProjectionDistance, igh_Behavior.Value.meshCrawlData.multiplier); } } else if (s == "Stigmergy") { totTail.AddRange(c.attributes.GetTrailVectors()); c.behaviors.SelfTailChase(igh_Behavior.Value.stigmergyData.viewAngle, igh_Behavior.Value.stigmergyData.cohesionMagnitude, igh_Behavior.Value.stigmergyData.cohesionRange, igh_Behavior.Value.stigmergyData.separationMagnitude, igh_Behavior.Value.stigmergyData.separationRange, totTail); } else if (s == "Noise") { if (igh_Behavior.Value.noiseData.colorMesh != null) { c.behaviors.Perlin2DMap(igh_Behavior.Value.noiseData.scale, igh_Behavior.Value.noiseData.strength, igh_Behavior.Value.noiseData.multiplier, igh_Behavior.Value.noiseData.velocity, igh_Behavior.Value.noiseData.mapStrength, igh_Behavior.Value.noiseData.mapStrength, igh_Behavior.Value.noiseData.mapMultiplier, igh_Behavior.Value.noiseData.colorMesh); } else { c.behaviors.Perlin(igh_Behavior.Value.noiseData.scale, igh_Behavior.Value.noiseData.strength, igh_Behavior.Value.noiseData.multiplier, igh_Behavior.Value.noiseData.velocity); } } else if (s == "Separation") { c.behaviors.Separate(igh_Behavior.Value.separationData.maxSeparation, creepList); } else if (s == "Force") { int forceAmount = igh_Behavior.Value.forceData.Count; for (int i = 0; i < forceAmount; i++) { if (igh_Behavior.Value.forceData[i].forceType == "Attract") { int attCount = 0; foreach (Point3d p in igh_Behavior.Value.forceData[i].targets) { c.behaviors.Attract((Vector3d)p, igh_Behavior.Value.forceData[i].thresholds[attCount], igh_Behavior.Value.forceData[i].attractionValue, igh_Behavior.Value.forceData[i].maxAttraction); attCount++; } } else if (igh_Behavior.Value.forceData[i].forceType == "Repel") { int attCount = 0; foreach (Point3d p in igh_Behavior.Value.forceData[i].targets) { c.behaviors.Repel((Vector3d)p, igh_Behavior.Value.forceData[i].thresholds[attCount], igh_Behavior.Value.forceData[i].repelValue, igh_Behavior.Value.forceData[i].maxRepel); attCount++; } } } } else { throw new Exception("Houston we have a problem, no behavior data read"); } } }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { object behavioral_Settings = null; object visual_Settings = null; bool reset = new bool(); List <Curve> curves = new List <Curve>(); if (!DA.GetDataList(0, curves)) { return; } if (!DA.GetData(1, ref behavioral_Settings) || behavioral_Settings == null) { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Behavior Input Object is Null"); return; } string objtype = behavioral_Settings.GetType().Name.ToString(); if (!(behavioral_Settings.GetType() == typeof(IGH_BehaviorData))) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "You did not input a Behavior Data Object, please ensure input is Behavior Data Object and not " + objtype); return; } if (!DA.GetData(2, ref visual_Settings) || visual_Settings == null) { AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Visual Input Object is Null"); return; } string objtype2 = visual_Settings.GetType().Name.ToString(); if (!(visual_Settings.GetType() == typeof(IGH_VisualData))) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "You did not input a Visual Data Object, please ensure input is Visual Data Object and not " + objtype2); return; } if (!DA.GetData(3, ref reset)) { return; } //------------------------Visual Settings-------------------------- IGH_VisualData igh_Visual = (IGH_VisualData)visual_Settings; TrailData td = igh_Visual.Value.trailData; ColorData cd = igh_Visual.Value.colorData; this.trail = td.createTrail; this.displayMode = igh_Visual.Value.displayMode; this.trailStep = td.trailStep; this.maxTrailSize = td.maxTrailSize; this.particleTexture = cd.particleTexture; this.graphicType = cd.colorDataType; this.useTexture = igh_Visual.Value.useTexture; if (cd.colorDataType == "Gradient") { this.maxthick = cd.maxThickness; this.minthick = cd.minThickness; this.redValues[0] = cd.redChannel[0]; this.redValues[1] = cd.redChannel[1]; this.greenValues[0] = cd.greenChannel[0]; this.greenValues[1] = cd.greenChannel[1]; this.blueValues[0] = cd.blueChannel[0]; this.blueValues[1] = cd.blueChannel[1]; } else if (cd.colorDataType == "GraphicPolyline") { this.polylineColor = cd.color; this.dotted = cd.dotted; this.maxthick = cd.maxThickness; } else if (cd.colorDataType == "Disco") { this.maxthick = cd.maxThickness; this.minthick = cd.minThickness; } else if (cd.colorDataType == "Base") { this.maxthick = 3; this.minthick = 1; } //----------------------------------------------------------------- if (reset) { this.cycles = 0; this.crvList = new List <Curve>(); this.crvList = curves; DA.SetDataList(0, crvList); } else { this.particleSet.Clear(); this.particleSet.TrimExcess(); IGH_BehaviorData igh_Behavior = (IGH_BehaviorData)behavioral_Settings; this.thresh = igh_Behavior.Value.bundlingData.threshold; this.ratio = igh_Behavior.Value.bundlingData.ratio; this.ptCount = igh_Behavior.Value.bundlingData.pointCount; this.rebuild = igh_Behavior.Value.bundlingData.rebuild; this.weldCount = igh_Behavior.Value.bundlingData.weldCount; //----------------------------------------------------------------- foreach (string s in igh_Behavior.Value.dataOrder) { if (s == "Bundling") { try { if (igh_Behavior.Value.bundlingData.colorMesh != null) { this.crvList = this.selfOrg.Bundling(particleList, particleSet, this.crvList, this.thresh, this.ratio, this.weldCount, this.rebuild, this.ptCount, igh_Behavior.Value.bundlingData.colorMesh, igh_Behavior.Value.bundlingData.useColor); } else { this.crvList = this.selfOrg.Bundling(particleList, particleSet, this.crvList, this.thresh, this.ratio, this.weldCount, this.rebuild, this.ptCount); } } catch (Exception e) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, e.Message.ToString()); return; } } else { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Currently we can only run bundling behavior on the Bundling Engine, please remove other behaviors"); return; } } if (this.displayMode == 1) { DA.SetDataList(0, this.crvList); } this.cycles++; } timer.DisplayMessage(this, "Bundling", this.cycles, this.myBool); }