C中的單文件波函數崩潰庫,加上命令行工具
這是一個支持重疊WFC方法的早期版本。該方法採用一個小的輸入圖像,並生成較大的輸出圖像,該圖像與輸入圖像局部相似。輸入/輸出對的一些示例:

WFC通常用於程序地圖生成,但不限於此用例。
該庫非常表現,包括其他實現中找不到的許多優化。例如,在MacBook Air M1(2020)上的上述128x128圖像(從3x3模式,翻轉和旋轉)的產生分別為:1.35、0.92、0.31、7.7、1.74和0.67秒。這包括圖像加載/節省時間。
您的項目中的一個文件應像這樣包括wfc.h :
#define WFC_IMPLEMENTATION
#include "wfc.h"其他文件也可以包括並使用wfc.h ,但它們不應該定義WFC_IMPLEMENTATION宏。
另外,您可以使用make wfc.o構建傳統的.O文件,並將wfc.h用作常規標頭文件。
用法:
struct wfc * wfc = wfc_overlapping (
128 , // Output image width in pixels
128 , // Output image height in pixels
input_image , // Input image that will be cut into tiles
3 , // Tile width in pixels
3 , // Tile height in pixels
1 , // Expand input image on the right and bottom
1 , // Add horizontal flips of all tiles
1 , // Add vertical flips of all tiles
1 // Add n*90deg rotations of all tiles
);
wfc_run ( wfc , -1 ); // Run Wave Function Collapse
// -1 means no limit on iterations
struct wfc_image * output_image = wfc_output_image ( wfc );
wfc_destroy ( wfc );
// use output_image->data
// wfc_img_destroy(output_image);默認情況下,您可以使用struct wfc_image用於輸入和輸出。
struct wfc_image {
unsigned char * data ;
int component_cnt ;
int width ;
int height ;
} data緊緊填充而無需填充。每個像素由component_cnt組件組成(例如,RGBA格式的四個組件)。輸出圖像將具有與輸入圖像相同數量的組件。
如果找不到解決方案,則wfc_run將返回0。您可以再次嘗試:
wfc_init ( wfc );
wfc_run ( wfc , -1 );WFC可以選擇使用STB_IMAGE.H和STB_IMAGE_WRITE.H,以提供直接與圖像文件合作的便利功能。
您通常將stb_image.h和stb_image_write.h放置在與wfc.h同一目錄中,並將其實現包括在一個項目文件中:
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image.h"
#include "stb_image_write.h"此外,您將指示wfc.h使用STB:
#define WFC_IMPLEMENTATION
#define WFC_USE_STB
#include "wfc.h"用法:
struct wfc_image * input_image = wfc_img_load ( "input.png" );
struct wfc * wfc = wfc_overlapping (
...
input_image ,
...
);
wfc_run ( wfc , -1 ); // Run Wave Function Collapse
// -1 means no restriction on number of iterations
wfc_export ( wfc , "output.png" );
wfc_img_destroy ( input_image );
wfc_destroy ( wfc );通過包含STB來啟用額外功能:
struct wfc_image * image = wfc_img_load ( "image.png" )
wfc_img_save ( image , "image.png" )
wfc_export ( wfc , "output.png" )
wfc_export_tiles ( wfc , "directory" )
// don't forget to wfc_img_destroy(image) loaded images 命令行工具使用庫並允許生成WFC圖像。該工具取決於stb_image.h和stb_image_write.h。將兩個文件都與wfctool.c相同的目錄放置。
make
./wfc
運行./wfc查看可用選項
基本用法:
./wfc -m overlapping samples/wrinkles.png output.png
感謝您使用WFC。如果您發現任何錯誤,有疑問或反饋,請告訴我。另外,如果您想分享自己的作品,這將非常感謝。
samp.krystian在gmail.com上