Skip to content

Instantly share code, notes, and snippets.

View cathode's full-sized avatar

William Shelley cathode

  • Geared Studios
  • Portland, OR
View GitHub Profile
private void InitMarchingCubesAlgorithm()
{
// We need to calculate a list of the poly counts for each configuration of 'solid' and 'air' vertices.
// There are eight vertices, each one representing a bit of a byte.
// Look-up array for polygon count value that indicates how many (up to 5) polygons we need to create.
var pc = new byte[256];
// Look-up array for polygon edge connections, indicating which edges we need to connect together.
var ec = new EdgeConnections[256];
// Vertex neighbors. Each vertex of a cube has three neighbor vertices.
/******************************************************************************
* Managed3D: A 3D Graphics API for .NET and Mono - http://gearedstudios.com/ *
* Copyright © 2009-2012 William 'cathode' Shelley. All Rights Reserved. *
* This software is released under the terms and conditions of the MIT/X11 *
* license. See the 'license.txt' file for details. *
*****************************************************************************/
using System;
using System.Runtime.InteropServices;
using System.Diagnostics.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Managed3D.Geometry;
using System.Diagnostics.Contracts;
using Managed3D.SceneGraph;
namespace Managed3D.Rendering.Software
{
private void InitMarchingCubesAlgorithmFancier()
{
// Even fancier. We'll use a LINQ query to build the lookup tables for Marching Cubes.
var result = from i in Enumerable.Range(0, 256)
let f = new Func<int, int>(s => (i & (1 << s)) >> s)
let verts = from j in Enumerable.Range(0, 8)
select f(j)
select verts; // not complete yet
}
@cathode
cathode / gist:5369905
Created April 12, 2013 06:26
comparison of contracts vs traditional input checking.
private void CheckUsage(object owner, string operationId)
{
// New code
Contract.Requires<InvalidOperationException>(owner == this.owner, "End was called on a different object than Begin");
Contract.Requires<InvalidOperationException>(null != this.operationId, "End was called multiple times for this operation.");
Contract.Requires<InvalidOperationException>(operationId == this.operationId, "End operation type was different than Begin.");
// Old code
if (!object.ReferenceEquals(owner, this.owner))
throw new InvalidOperationException("End was called on a different object than Begin");
@cathode
cathode / gist:5378197
Created April 13, 2013 12:26
Does this even work?
public Frame GetFrame(long index)
{
IAsyncResult result = this.BeginGetFrame(null, index);
var frame = this.EndGetFrame(result);
return frame;
}
@cathode
cathode / gist:5398880
Created April 16, 2013 19:32
Code contracts static analyzer is complaining about "Warning 2411 CodeContracts: Possibly calling a method on a null reference C:\Users\cathode\Documents\Visual Studio 2012\Projects\nsynth\source\NSynth\FilterInputFrames.cs 47 35 NSynth" on line 21. Presumably it thinks "owner" might be null, which I dont see how it could be.
/// <summary>
/// Initializes a new instance of the <see cref="FilterInputFrames"/> class.
/// </summary>
/// <param name="owner">The filter that is rendering a frame to which the input frames will be used.</param>
/// <param name="currentIndex">The frame index being rendered.</param>
public FilterInputFrames(Filter owner, long currentIndex)
{
Contract.Requires(owner != null);
Contract.Requires(currentIndex >= 0);
public unsafe void WriteToBitmap(System.Drawing.Bitmap bitmap)
{
Contract.Requires(bitmap != null);
var bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
unchecked
{
int* ptr = (int*)bmpData.Scan0.ToPointer();
fixed (Vector4f* cptr = &(this.Pixels[0]))
/// <summary>
/// Gets the position on the current polygon at which the specified edge intersects it.
/// </summary>
/// <param name="line"></param>
/// <returns></returns>
public Vector3 GetIntersection(Edge3 line)
{
// Set up vector variables;
Vector3 a, b, c, p, q;
a = this.A.ToVector3();
@cathode
cathode / gist:6191280
Created August 9, 2013 04:55
DataBuffer example
TGABitstreamHeader header;
header.IdLength = buffer.ReadByte();
header.ColorMapType = (TGAColorMapType)buffer.ReadByte();
header.ImageType = (TGAImageType)buffer.ReadByte();
header.ColorMapOrigin = buffer.ReadUInt16();
header.ColorMapLength = buffer.ReadUInt16();
header.ColorMapDepth = buffer.ReadByte();
header.XOrigin = buffer.ReadUInt16();
header.YOrigin = buffer.ReadUInt16();
header.Width = buffer.ReadUInt16();